Hello i have a table with 10 rows in total where 6 have ID 1
and 4 have ID 2
i want to count only the rows with ID 1
but my code counts all of them in total. This is my code:
$clientOrder = clientHasOrder::where('order_id', $id)->firstOrFail();
$rowCount = $clientOrder->count();
dd($rowCount);
If the code worked correct i should get 6 rows with specific $id
but this code gives 10 rows that are total in the table.
If i do dd(clientHasOrder::all());
i get:
This is the result in details as you can see i get the 10 results but i need only 6 rows with specific 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
}
CodePudding user response:
You can invoke count directly on the query, like this:
$clientOrder = clientHasOrder::where('order_id', $id)->count();
dd($clientOrder);
And that will show the correct amount, you do not need to firstOrFail, because if the results are empty, it will show 0 count
.