I have a User model that does have many Networks. Each Network does have many Lists.
I have this code in the User model that is really slow:
foreach ($this->networks as $network) {
if ($myCondition) {
foreach ($network->lists()->get()->lists('id')->toArray() as $newId) {
$ids[] = $newId;
}
}
}
I wonder if there is a way to load all lists of all networks before the 2 foreach loops.
This may speed it up, although I'm not certain:
$this->networks()->with('lists')->get()->pluck('lists')->flatten()->pluck('id')
Also if it isn't a many to many relationship you could consider putting user_id as a column in your 'lists' table if speed is really important
This is lazy eager loading:
$this->networks->load('lists');
That loaded the lists relationship for all the networks.
If you just want this list of ids and don't need the actual records returned you can do that as well:
$ids = Lists::whereHas('network', function ($query) {
$query->whereIn('id', $this->networks->pluck('id'));
})->pluck('id');
If you want because Lists belongsTo Network you can go around the relationship:
$ids = Lists::whereIn('network_id', $this->networks->pluck('id'))
->pluck('id');
Just keep code in User model. In controller, you can do:
$User->load('networks', function($q){
$q->with('lists');
});
There in:
'networks' and 'lists' is your relationship name.
Further:
You can lazy load all your relation ship, in child by child.
$User->load('networks', function($q){
$q->with('lists', function($q1){
$q1->with('child_relationship_of_lists');
});
});
Related
I have two db tables: exercises and schedules. The relation between them is a many to many.
I have some data stored in the pivot table. When i return the table i don't want id to be shown, because it is a useless repetition. The code i'm using:
$results = Schedule::with(['exercises', 'exercises.tags'])->where('user_id', Auth::user()->id)
->get();
$results->each(function ($schedule) {
$schedule->exercises->each->makeVisible('pivot');
});
I tried this approach, but it doens't seem to work:
$results = $results->toArray();
foreach ($results as $schedule) {
foreach ($schedule['exercises'] as $exercise) {
unset($exercise['pivot']['schedule_id']);
unset($exercise['pivot']['exercise_id']);
}
}
I know the question may not be much clear, sorry for that but my english is not the best.
Use the eloquent makeHidden method:
$results = $results ->makeHidden(['exercises.pivot.schedule_id']);
source: https://laravel.com/docs/8.x/eloquent-collections#method-makeHidden
I have comments table where has parent_id
This is Comment table sub_comments relation.
public function sub_comments()
{
return $this->hasMany(self::class, 'parent_id');
}
This code return all comments with related all sub-comments
Comment::with('sub_comments')->get();
But I want to get all comments also sub-comments when sub-comments is single. That mean if comment have 2 or more comments for that comment I did not want get that sub-comments.
Now I use this code
$oneSubcommentCommentIds = Comment::has('sub_comments', '=', 1)->pluck('id');
Comment::with([
'sub_comments' => function ($q) use ($oneSubcommentCommentIds) {
$q->whereIn('parent_id', $oneSubcommentCommentIds);
}
])->get();
but this make one additional query.
Try this:
Comment::with('sub_comments')->has('sub_comments', '=', 1)->get();
Update
Your question wasn't clear, I can't imagine another way to doing this without previosly loaded the relationship or the count of the relationship.. so I'd do this:
// First get all your comments with an aditional count field
$comments = Comments::withCount('sub_comments')->get();
// separate the ones with just one sub_comment from the rest
list($oneSubComment, $theRest) = $collection->partition(function ($comment) {
return $comment->sub_comments_count == 1;
});
// Then load the relationship on just the selected elements
$oneSubComment->load('sub_comments');
// re-join the collection
$comments = $oneSubComment->union($theRest);
What am I doing here?
Adding an additional field to each $comment with the relationship count (it should be something like sub_comments_count)
Partition the resulting collection in two parts: the ones with one comment and the rest. Using the partition() method.
Lazy eager loading the collection.
Re-joining the two collections using the union() method.
sorry for the title of this question but I am not sure how to ask it...
I am working on a project where I have two Models Trains and Cars, to this model I have a belonging Route.
I want to make a query and check if the routeable_type is App\Car than with the selected routeable_id to get the data from the Car. And if the routeable_type is Train then with the ID to get the data from the Tran.
So my models go like this:
Train:
class Train extends Model
{
public function routes()
{
return $this->morphMany('App\Route', 'routeable');
}
}
Car:
class Car extends Model
{
public function routes()
{
return $this->morphMany('App\Route', 'routeable');
}
}
Route:
class Route extends Model
{
public function routeable()
{
return $this->morphTo();
}
}
And the query I have at the moment is:
$data = Route::leftjoin('cars', 'cars.id', '=', 'routes.routeable_id')
->leftjoin('trains', 'trains.id', '=', 'routes.routeable_id')
->select('routes.id', 'cars.model AS carmodel', 'trains.model AS trainmodel', 'routeable_type', 'routes.created_at');
With this query if I have the same ID in cars and trains I get the data from both and all messes up. How do I check if routeable_type is Car ... do this, if routeable_type is Train .. do that?
Will something like this be possible in a 1 single query:
$data = Route::select('routes.id', 'routeable_type', 'routes.created_at');
if(routeable_type == 'Car'){
$data = $data->leftjoin('cars', 'cars.id', '=', 'routes.routeable_id')->select('routes.id', 'cars.model AS carmodel', 'routeable_type', 'routes.created_at');
}else{
$data = $data->leftjoin('trains', 'trains.id', '=', 'routes.routeable_id')->select('routes.id', 'trains.model AS trainmodel', 'routeable_type', 'routes.created_at');
}
Maybe this is what you are looking for?
DB::table('routes')
->leftJoin('cars', function ($join) {
$join->on('cars.id', '=', 'routes.routeable_id')
->where('routes.routeable_type', 'App\Car');
})
->leftJoin('trains', function ($join) {
$join->on('trains.id', '=', 'routes.routeable_id')
->where('routes.routeable_type', 'App\Train');
})
->select('routes.id', 'cars.model AS car_model', 'trains.model AS train_model', 'routes.routeable_type', 'routes.created_at');
->get();
I think you may want to follow the morphedByMany design.
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many-polymorphic-relations
This was also a neat visual for the different relation types.
https://hackernoon.com/eloquent-relationships-cheat-sheet-5155498c209
I was faced with a similar issue though I failed to follow the correct design initially and was forced to query the many possible relations then wrote custom logic after to collect the relation types and ids then do another query and assign them back through iteration. It was ugly but worked... very similar to how Eloquent does things normally.
i don't have enough repo, so i can't comment. that's why i am putting as an answer.
You should use 2 different queries, for each model.
This will be better, code wise as well as performance wise. also if both models have similar fields you should merge them to 1 table and add a 'type' column.
and put non-similar fields in a 'meta' column.
( in my opinion )
Is there any way to return a parent model with a relationship but only return some of the relationship rows by using a where in?
That may be quite confusing, let me explain.
At the moment I have 2 models, Buildings and rooms, 1 building can have many rooms.
I want to be able to pass in an array of room ids, and return the sites and only the rooms that are in the array.
Heres what I have at the moment
if($request->input('ids') && !is_null($request->input('ids'))){
$ids = explode(',',$request->input('ids'));
//Exploded ids looks like this "2,4,11,55,56"
$buildings = Buildings::join('rooms')->whereIn('rooms.id',$ids)->get();
} else {
$buildings = Buildings::whereHas('rooms')->get();
}
At the moment this will return all buildings that have a room which id is in the ids array and all of its rooms, which ends up returning a building with 200+ rooms. I need it to return the building and ONLY the rooms that have an id in that array.
Is this possible?
I know I can do it the inverse way and get all rooms as the parent then get the buildings, but I need buildings to be the parent as i'm running a foreach like this with the results
foreach($buildings as $key => $building){
<h1>{{$building->name}}</h1>
foreach($building->rooms as $k => $room){
<p>{{$room->name}}</p>
}
}
Incase thats still confusing, the real world scenario is that i'm generating a PDF of rooms. The rooms can be selected by ticking a checkbox next to the room in a room list. I then need to be able to pass the array of room ids, and get all buildings that contain one of the rooms. Then get all of the rooms for each building where the room id is in the array.
First you need to know, whereHas only filter your parent result but not the eager loading relation. So you need to apply that filter in eager load too. Like this
$ids = explode(',',$request->input('ids'));
$buildings = Buildings::with(['rooms' => function($q) use ($ids) {
$q->whereIn('id', $ids);
}])->whereHas('rooms', function($q) use ($ids) {
$q->whereIn('id', $ids);
})->get();
Here whereHas filter buildings and using with filter rooms.
$ids = explode(',',$request->input('ids'));
$building_ids = Room::whereIn('id',$ids)->pluck('building_id');
$buildings_with_specific_rooms = Building::join('rooms', 'buildings.id', '=', 'rooms.building_id')->select('buildings.name', 'rooms.name')->whereIn('buildings.id', $building_ids)->whereIn('rooms.id', $ids)->get();
hope this helps you.
you can do this with the following code:
$ids = explode(',',$request->input('ids'));
$buildings = Buildings::whereHas('rooms', function($q) use ($ids) {
$q->whereIn('id', $ids);
})->get();
Hope this helps.
You can eager load the child relation and get the buildings out of the rooms collection:
$buildings = Room::with('building')
->with('building.room')
->whereIn('id', $ids)
->get()
->pluck('building');
For this to work you need to have the relation declared in both Building and Room models.
Laravel 4
Is there any easy way to get all related models in one collection (only with Eloquent)?
For example, I want to get all of the students, that is related to many classes:
First, I need to get a collection with the relations:
$classes = Class::with('students')->whereIn('code', ['A', 'B'])->get();
Then I need to go through the entire collection to merge the students:
$allStudents = new Collection;
foreach ($classes as $class) {
$allStudents = $allStudents->merge($class->students);
}
Or if I need only one key, for example, id of the student, I'm doing this:
$allStudentsIds = [];
foreach ($classes->fetch('students') as $studentsArray) {
$allStudentsIds = array_merge(
$allStudentsIds, array_pluck($studentsArray, 'id')
);
}
Is there a method to get only related models? Or somehow I can make it through a request to the Student model?
Query based on students and join with classes?
Student::select('student.*')
->join('class_student', 'class_student.student_id', '=', 'student.id')
->join('class', 'class.id', '=', 'class_student.class_id')
->whereIn('class.id', $classes)
->get();
Something like that.
I assumed it is a many to many relationship and class_student is the association table