How to get counts from multiple Models eloquent [Laravel 5] - laravel-5

I know how to get records count for a model :
User::count();
But how can I get counts from multiple models in one eloquent query?

Related

Laravel get relationship of relationship

I am trying to retrieve additional data from a relation. But so far I have not been able to do this. It is about a group that contains products. I want to retrieve data related to these products. So far I have not found a solution to realise this.
Tables with fields:
lager_stock_booking_groups
id,datetime
lager_stock_booking
id, lagerProductId, stockBookingGroupId(id from Table lager_stock_booking_groups)
lager_products
id(lagerProductId from Table lager_stock_bookings)
$bookingGroup = LagerStockBookingGroup::with('stockBookingProducts')->where('id', '=', $id)->orderBy('dateTime', 'desc')->get();
This code get me the groups and Products liek this:
Group[ID 23]
Product 1
Product 2
Product 3
I have only the Product-ID's. How can get additional Data of Products from Table lager_products?
Thank you for your help.
you can get with relation :
$bookingGroup->stockBookingProducts
or use join

Eager loading (withCount), limit on how many models you can preload?

I'm having an issue where I have to display a table rendered client side, but the returned number of rows is about 2.5k entries. The model has a many-to-many relationship with a Sentence model through a bridging/pivot table with about 7k rows:
public function sentences()
{
return $this->belongsToMany(Sentence::class)->withTimestamps();
}
I'm trying to preload the Sentences via $entries = Entry:::with('sentences')->get(); but this generates a query with 2.5k ids in it where entry_sentence.entry_id in (1, 2, 3, 4, 5, 7, ..., 2462, 2463, 2464).
This query generated runs in about 0.06s on my local machine. But it takes about 8 seconds to generate the collection, which I think is due to hydration? Without preloading its very fast, but then I run into n+1 issues looping over my rows.
Am I forced into either "raw" DB::table() queries without Eloquent or pagination (and thus server side filtering ect.) ? What is the reasonable limit on eager loaded relationships? I can't seem to find any advice on this anywhere. I'm using Datatables in frontend and their rule of thumb was doing AJAX until about 5k rows then you should consider going server side pagination instead.
You can customize the related query:
$entries = Entry:::with('sentences' => function($query) {
return $query->limit(5); // however many is useful
}])->get();

ManyToMany with and whereIn

I have a ManyToMany relationship between AdInterest and AdInterestGroup models, with a belongsToMany() method in each model so I can use dynamic properties:
AdInterest->groups
AdInterestGroup->interests
I can find all the "interests" in a single group like this:
$interests = AdInterestGroup::find(1)->interests->pluck('foo');
What I need is a merged, deduplicated array of the related 'foo' field from multiple groups.
I imagine I can deduplicate with ->unique(), but first, as you'd expect, this:
AdInterestGroup::whereIn('id',[1,2])->interests->get();
throws:
Property [interests] does not exist on the Eloquent builder instance.
The advice seems to be to use eager loading via with():
AdInterestGroup::with('interests')->whereIn('id',[1,2])->get();
Firstly, as you'd expect that's giving me an array of two values though (one for each ID).
Also, if I try and pluck('foo') again, it's looking in the wrong database table: from the AdInterestGroup table, rather than the relationship (AdInterest).
Is there a nice, neat Collection method / pipeline I can use to combine the data and get access to the relationship fields?
Use pluck() and flatten():
$groups = AdInterestGroup::with('interests')->whereIn('id', [1, 2])->get();
$interests = $groups->pluck('interests')->flatten();
$foos = $interests->pluck('foo')->unique();

Optimize query Eloquent laravel for thousands records

I have a problem with a query in Laravel with many records, because it is so slow.
I have a table users that has 4934 records.
I have table of relations that for example is named user_relation_values that has 17482 records.
I have a table values that has 20495 records.
Now I receive,from front end, N id of users, so I could receive 1 id or 4934 ids or 2000 ids or 1000 ids, so I don't know how many ids I will receive.
I have to return, starting by ids received, the relations of model users with id.
So in My backend I have a function like this:
$users= $request->input('users');
$usersValues = array();
foreach ($users as $user){
$o = User::find($owner['id']);
$o->values;
$owersProperties[] = $o;
}
I have the relations in My User model:
public function values(){
return $this->belongsToMany('App\Models\Values');
}
I have put The various indexes on tables but the query Is so slow if I receive the all id.
If I receive all 4934 ids the query takes more than 20 seconds, but I read that someone make query for milions of records in just over 5 seconds.
How can I optimize my table or my query?
Looking at your example code, you’re query users in a loop (n queries), and then also querying values for each user. That’s going to issue an exponential number of queries. Instead, you should query the users and eager-load the values relationship.
If you’re getting an array of user IDs as input, then you can pass that to Eloquent’s find() method:
$users = User::find($request->input('users'));
You should also eager-load the values relationship if you plan on using it in a loop:
$users = User::with('values')->find($request->input('users'));
This should dramatically reduce the number of queries you issue.

Laravel eloquent - get the count of rows of the pivot table in many to many relationship

I need to get the count of all the rows in the pivot user_engagement. I have defined a many to many relationship for users and engagements. I am getting the number with a raw query at the moment, but would like to change this query in to an eloquent one if possible:
$numberOfEngagements = DB::table('user_engagement')->count();
Create model for your many to many relationship table and try this
$count = App\UserEngagement::all()->count();
Faster and memory friendly:
$count = App\UserEngagement::count();

Resources