Reputation: 100
I got a response as follows. I need to the print the "count(*)" field along with some other data like "Duke (2)" where 2 is the count. How can I print the value in the laravel template.
{
parts_model_id: 8,
parts_id: 29,
parts_title: "duke multiple model",
model_id: 1,
model_name: "OPAH2",
created_at: "2017-07-18 17:02:10",
updated_at: "2017-07-18 17:02:10",
count(*): 2
}
The code
$models = PartsModel::with('model')->selectRaw('*, count(*)')->groupBy('model_id')->get();
Thank you
Upvotes: 1
Views: 264
Reputation: 11083
You can do it like this :
$models = PartsModel::with('model')
->selectRaw('*, count(*) AS countElements')
->groupBy('model_id')
->get();
And you can access the count using (after looping :)) :
$model->countElements;
Upvotes: 1