如何计算具有特定ID的行数Laravel

vwhgwdsa  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(145)

你好,我有一个表,总共有10行,其中6个有ID 1,4个有ID 2我想只计算ID 1的行,但我的代码计算所有这些行的总数。

$clientOrder = clientHasOrder::where('order_id', $id)->firstOrFail();
        $rowCount = $clientOrder->count();
        dd($rowCount);

如果代码工作正确,我应该得到6行特定的$id,但这个代码给出了10行是在表中的总数。
如果我做dd(clientHasOrder::all());,我得到:

这是详细的结果,如您所见,我得到了10个结果,但我只需要6行具有特定ID

Illuminate\Database\Eloquent\Collection {#1367 ▼ // app/Http/Controllers/OrderController.php:121
  #items: array:11 [▼
    0 => App\Models\clientHasOrder {#1369 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "36"
        "product_id" => 1
        "amount" => "10"
      ]
      #original: array:3 [▼
        "order_id" => "36"
        "product_id" => 1
        "amount" => "10"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    1 => App\Models\clientHasOrder {#1370 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "36"
        "product_id" => 2
        "amount" => "20"
      ]
      #original: array:3 [▼
        "order_id" => "36"
        "product_id" => 2
        "amount" => "20"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    2 => App\Models\clientHasOrder {#1371 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "36"
        "product_id" => 3
        "amount" => "30"
      ]
      #original: array:3 [▼
        "order_id" => "36"
        "product_id" => 3
        "amount" => "30"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    3 => App\Models\clientHasOrder {#1372 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "36"
        "product_id" => 4
        "amount" => "40"
      ]
      #original: array:3 [▼
        "order_id" => "36"
        "product_id" => 4
        "amount" => "40"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    4 => App\Models\clientHasOrder {#1373 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "37"
        "product_id" => 7
        "amount" => "10"
      ]
      #original: array:3 [▼
        "order_id" => "37"
        "product_id" => 7
        "amount" => "10"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    5 => App\Models\clientHasOrder {#1374 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "37"
        "product_id" => 8
        "amount" => "20"
      ]
      #original: array:3 [▼
        "order_id" => "37"
        "product_id" => 8
        "amount" => "20"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    6 => App\Models\clientHasOrder {#1375 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "37"
        "product_id" => 9
        "amount" => "30"
      ]
      #original: array:3 [▼
        "order_id" => "37"
        "product_id" => 9
        "amount" => "30"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    7 => App\Models\clientHasOrder {#1376 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "37"
        "product_id" => 10
        "amount" => "40"
      ]
      #original: array:3 [▼
        "order_id" => "37"
        "product_id" => 10
        "amount" => "40"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    8 => App\Models\clientHasOrder {#1377 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "38"
        "product_id" => 1
        "amount" => "10"
      ]
      #original: array:3 [▼
        "order_id" => "38"
        "product_id" => 1
        "amount" => "10"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    9 => App\Models\clientHasOrder {#1378 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "38"
        "product_id" => 7
        "amount" => "20"
      ]
      #original: array:3 [▼
        "order_id" => "38"
        "product_id" => 7
        "amount" => "20"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▶]
    }
    10 => App\Models\clientHasOrder {#1379 ▼
      #connection: "mysql"
      #table: "client_has_orders"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:3 [▼
        "order_id" => "38"
        "product_id" => 9
        "amount" => "30"
      ]
      #original: array:3 [▼
        "order_id" => "38"
        "product_id" => 9
        "amount" => "30"
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: false
      #hidden: []
      #visible: []
      #fillable: array:3 [▼
        0 => "order_id"
        1 => "product_id"
        2 => "amount"
      ]
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
  ]
  #escapeWhenCastingToString: false
}
rhfm7lfc

rhfm7lfc1#

您可以直接在查询上调用count,如下所示:

$clientOrder = clientHasOrder::where('order_id', $id)->count();

dd($clientOrder);

这将显示正确的数量,您不需要firstOrFail,因为如果结果为空,它将显示0 count

相关问题