How to display count(*) in laravel 5.2 - laravel

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

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;

Related

How do I count the number of Items in a grouped collection in Laravel?

Please I want to count the number of Items in each grouped collection. assuming the collection returns three group Items, I want to count the collection in each group
I did this but it isn't working out for me
$collection = Vendor::all();
$grouped = $collection->groupBy('vendor_discount');
$counted = $grouped->countBy();
return $counted->all();
this is the error am getting back
array_key_exists(): The first argument should be either a string or an integer
by doing this
$user_info = DB::table('vendors')
->select('vendor_discount', DB::raw('count(*) as total'))
->groupBy('vendor_discount')
->get();
this is the results
0: {vendor_discount: null, total: 1}
total: 1
vendor_discount: null
1: {vendor_discount: "10%", total: 2}
total: 2
vendor_discount: "10%"
2: {vendor_discount: "20%", total: 2}
total: 2
vendor_discount: "20%"
how do I correctly display in a view template?
Your Query
$user_info = DB::table('vendors')
->select('vendor_discount', DB::raw('count(*) as total'))
->groupBy('vendor_discount')
->get()->toArray();
return $user_info;
then in the view file
//Loop through the values
foreach($user_info as $user){
echo $user->vendor_discount." - ".$user->total;
}

Laravel collection->diff() not working when using select

I have table where I want to find difference in certain columns. I'm doing it with diff():
$inventoryitems1 = InventoryItem::where('inventory_id', $request->get('inventory1'))
->select('owner_id', 'location_id', 'asset_id')
->get();
$inventoryitems2 = InventoryItem::where('inventory_id', $request->get('inventory2'))
->select('owner_id', 'location_id', 'asset_id')
->get();
$difference = $inventoryitems2->diff($inventoryitems1);
And then i get empty array. But if I don't have select in my collection. It work, it shows all different rows. Is there anyother way to do this or this one is right but I'm not doing it the right way?
UPDATE
I have $inventoryitems1 collection that looks like this:
0: {asset_id: 2, owner_id: 2, location_id: 1}
1: {asset_id: 3, owner_id: 2, location_id: 1}
and $inventoryitems1 collection that looks like this:
0: {asset_id: 2, owner_id: 2, location_id: 1}
1: {asset_id: 3, owner_id: 6, location_id: 1}
I'm trying to find differences in those two collections, where we can see that owner_id is different in second collection. How can I only get those objects where there was some change (difference).

Laravel Eloquent querying pivot table not unique

For some reason I don't get the expected result when doing the following:
$cars = Car::query();
$cars->whereIsVisible(true);
# output: $list
// [2016-01-16 09:30:04] local.INFO: array (
// 0 => 5,
// 1 => 7,
// 2 => 9,
// 3 => 3,
// )
$cars->whereHas('specifications', function($query) use ($list) {
$query->whereIn('id', ($list));
});
$cars->get();
What I expect is that I get only cars that have all the specifications that are inside that $list, but that's not correct. Even when I fill in more specifications, I get a bigger result. So there goes something wrong.
I'm used to Eloquent, so I suck in queries. But this is the Query:
select * from "cars" where "cars"."deleted_at" is null
and "is_visible" = true
and (select count(*)
from "specs" inner join "car_specs"
on "specs"."id" = "car_specs"."facility_id"
where "car_specs"."car_id" = "cars"."id"
and "id" in (5, 7, 9 ,3)) >= 1
Anyone see where it goes wrong? And how to fix it?
According to https://laravel.com/docs/5.1/eloquent-relationships#querying-relations
whereHas just checks that you have cars with the provided specifications, not that it returns those cars that match your specifications.
I think you should use where directly.

Eloquent with(), select() and where() selection

This is my query to fetch data from 2 different table.
$variant = Variant::with(['v_detail' => function($q){
$q->select('variant_dtl_name');
}])->where('product_id','=',$productId)->get();
There is output, but v_detail returning empty list
result:
created_at: "2015-11-07 12:37:26"
id: 1
product_id: 30
updated_at: "2015-11-07 12:37:26"
v_detail: []
variant_name: "Pricing"
But with these query:
$variant = Variant::with('v_detail')->where('product_id','=',$productId)->get();
The result is:
created_at: "2015-11-07 12:37:26"
id: 1
product_id: 30
updated_at: "2015-11-07 12:37:26"
v_detail: [{id: 1, variant_id: 1, variant_dtl_name: "Adult", variant_dtl_price: 25,…},…]
0: {id: 1, variant_id: 1, variant_dtl_name: "Adult", variant_dtl_price: 25,…}
1: {id: 2, variant_id: 1, variant_dtl_name: "Senior", variant_dtl_price: 15,…}
2: {id: 3, variant_id: 1, variant_dtl_name: "Children", variant_dtl_price: 8,…}
variant_name: "Pricing"
Now, on the query that work, how can I fetch a specific column names. Thanks!!
You have this:
$variant = Variant::with(['v_detail' => function($q)
{
// Either add the related foreign key or select all
$q->select('related_foreign_key', 'variant_dtl_name');
}])->where('product_id','=',$productId)->get();
Since you are selecting only a single field which is variant_dtl_name then it's not possible to find out the related models because the relation builder foreign key is required. So, you have to select that foreign key as well. Notice the related_foreign_key in sub-query so use the right one, probably that is variant_id but not sure because you didn't mention anything about that.

Kohana Database Query Builder custom sort order ("ORDER BY Field (id, 1, 3, 2)" in MySQL)

I have a set of id's to select, so I request:
$ids = array( 1, 2, 3, 4, 5 );
$q = DB::select('field1', 'field2', 'field3')->
from('work')->
where('field1', 'in', $ids)->execute();
How can I sort them in my custom order, like MySQL's 'ORDER BY Field' do?
Check out DB::Expr
You can use it like so:
->order_by(DB::Expr('FIELD(`field`, 3,1,2)'))
Note, you'll have to manually escape the contents

Resources