您好,我有一个总共有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
}