Get all objects that exist in two collections - laravel

I'm building a Laravel page on which I want to show a list of lessons. Which lessons should be on the page is filtered by three criteria (of which all should be true):
The lesson is active, ie "where('active', true)". Simple enough.
The lesson is part of a track that the user has chosen. Models are set up with belongsToMany() (it is a many-to-many relationship), so I can get these lessons by a simple $track->lessons.
This is where it gets tricky. Some lessons should only be visible to users with certain titles (ie there is a many to many between titles and lessons). I can get the lessons with the correct title requirement using Auth::user()->title->lessons.
Question is how I get all this together. The best I've come up with this far is the following:
$title = Auth::user()->title;
$lessons = Lesson::where('active', true)
->whereIn('id', $track->lessons->pluck('id'))
->where(function ($query) use($title) {
$query->whereIn('id', $title->lessons->pluck('id'))->orWhere('limited_by_title', false);
})
->get();
...which is crap ugly, clearly suboptimal and (for some reason I really don't understand) also won't work (I don't get the lessons my title entitles me to in my list). Been struggling for quite some hours now, I get the feeling that I'm overcomplicating, first plucking id's and then using them in a whereIn() can't possibly be a good way of doing this.
So I can easily enough get a collection of lessons in the track, and I can get a collection of lessons belonging to the title, but how do I get all objects that exist in both those collections?

Using whereHas() is the answer to your concerns about plucking IDs. Instead of running additional queries to retrieve IDs, whereHas() will attach the constraint to the original query as a subquery on the related tables.
Breaking the query down to its parts:
1: Answered
2: Assuming the inverse of $track->lessons is $lesson->tracks, and $track is coming from code you didn't include:
$lessons = Lesson::whereHas('tracks', function ($query) use ($track) {
$query->where('id', $track->id);
})
3: Assuming the inverse of $title->lessons is $lesson->titles:
$lessons = Lesson::where(function ($query) use ($title) {
$query->whereHas('titles', function ($query) use ($title) {
$query->where('id', $title->id);
})
->orWhere('limited_by_title', false);
})
Combined back into one:
$track = ???;
$title = Auth::user()->title;
$lessons = Lesson::where('active', true)
->whereHas('tracks', function ($query) use ($track) {
$query->where('id', $track->id);
})
->where(function ($query) use ($title) {
$query->whereHas('titles', function ($query) use ($title) {
$query->where('id', $title->id);
})
->orWhere('limited_by_title', false);
})
->get();
If this still doesn't give the results you were expecting, you can examine the full query being run by replacing get() with toSql(). Sometimes working from the ORM as a starting point instead of the SQL can lead you down the wrong path. For even more detail to debug and understand the queries being run, you can enable query logging: https://laravel.com/docs/5.7/database#listening-for-query-events

intead of where use whereHas on "titles" relationship
$title = Auth::user()->title;
$lessons = Lesson::where('active', true)
->whereIn('id', $track->lessons->pluck('id'))
->whereHas('titles',function ($query) use($title) {
$query->whereIn('id', $title->pluck('id'))
->orWhere('limited_by_title', false);
})->get();

First of all complicated, really complicated. Your table structure needs serious modification to make it easier.
However, considering you don't want to go down that road, you could do it simpler by using join
Assuming you have following table structure:
users
titles (has user_id foreign key)
lessons (has title_id foreign key)
tracks (has lesson_id foreign key)
$trackName = $request->input('track_name');
$title = Auth::user()->title;
$lessons = Lesson::join('tracks', 'lessons.id', '=', 'tracks.lesson_id')
->join('titles', 'lessons.title_id', '=', 'titles.id')
->where('lessons.active', true)
->where('tracks.track_name', $trackName)
->where(function ($query) use($title) {
$query->where('titles.id', $title->id)->orWhere('lessons.limited_by_title', false);
});
dd($lessons);
That is of course if your users table and titles have one to one relationship otherwise pluck all title_ids and use whereIn instead of where for titles.id query.
I hope you have enough understanding of laravel framework to understand and implement this solution.
Sorry, I don't have enough time to proofread or give more details.
Good luck!
Good luck if you need pagination after that :p I doubt simple ->paginate() will work :D
I hope it helps

Related

Laravel Eloquent - orWhereHas method - When to use it and how

I'm trying to understand some advanced eloquent commands and on the Laravel official documentation, there is not so much about the Eloquent orWhereHas method and there isn't also an example about how it works.
https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
Can somebody help me to understand it with also a simple example?
How to use it: just chain as any other Eloquent method
User::whereHas(...)->orWhereHas(...)->get();
When to use it: imagine you have Users, Posts and Comments, and each user can write posts and comments. Then you need to get active users. For example, you assume active as user, who has made posts OR comments last 7 days. So, you can get it this way:
$users = App\Models\User::query()
->whereHas('posts', function (Builder $query) {
$query->where('created_at', '>=', Carbon::now()->subDays(7));
})
->orWhereHas('comments', function (Builder $query) {
$query->where('created_at', '>=', Carbon::now()->subDays(7));
})
->get();
Say there's a blog kind of app. The main entity/model of the app would be Post (Blog Post).
When any author writes and publishes a Post,
visitors to the blog site can leave Comment(s) for the Post
visitors can Like a Post
So we have 3 models here
Post - which can have many Comment(s)
Post - can have many Like(s)
Now let's say for some reason we want to get all Post records from the database which either have 10 or more comments in the current month or 3 or more likes in the current month
We can write a query like
$posts = Post::whereHas('comments', function($query) {
$query->where('created_at', '>', now()->startOfMonth();
}, '>=', 10)
->orWhereHas('likes', function($query){
$query->where('created_at', '> ', now()->startOfMonth();
}, '>=', 3)
->get();
Laravel docs: https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
Just like where both whereHas and orWhereHas accepts closure as 2nd argument for more fine grained query control.
Actually whereHas is supposed to be used when you want to have more power on constraints.
If you just want to check the existence of relation records you can use has for eg:
Get all post records which either have comment or like and paginate 20 per page
$postsWithCommentsOrLikes = Post::has('comments')
->orHas('likes')
->paginate(20);

Refactor whereHas to use via global scopes in Laravel

I've 2 models, CoreChallenge and Challenge. Below are relations between table.
I want to fetch Challenges which has core_challenges active. I tried to do putting global scope in CoreChallenge model, but when I'm getting null in relationship when Corechallenge is inactive.
I've done it this way
$challenges = Challenge::with('core_challenge')->whereHas('core_challenge', function($q){
$q->where('status', '=', 'active');
})->get();
I want to do it using global scopes
Global scope on CoreChallenge gives me null, but I want that it's parent (Challenge) should not load even, like in whereHas. Is there any way?
I have stumbled to the same approach, but when the table got bigger (core_challenges_table in your scenario), whereHas ended up being very slow (around 1min response time).
So I used a solution like this:
$ids = CoreChallenge::where('status', 'active')->pluck('id');
$challenges = Challenge::with('core_challenges')
->whereIn('core_challenge_id', $ids)
->get();
With this approach, my query reduced to 600~ms from 1min.
Which can be translated to Model scopes
class Challenge {
public function scopeActive($query) {
$activeIds = CoreChallenge::where('status', 'active')->pluck('id');
return $query->whereIn('core_challenge_id', $ids);
}
}
Challenge::with('core_challenges')->active()->get();

Where clause inside whereHas being ignored in Eloquent

Im trying to make a query using whereHas with eloquent. The query is like this:
$projects = Project::whereHas('investments', function($q) {
$q->where('status','=','paid');
})
->with('investments')
->get();
Im using Laravel 5.2 using a Postgres driver.
The Project model is:
public function investments()
{
return $this->hasMany('App\Investment');
}
The investments model has:
public function project() {
return $this->belongsTo('App\Project');
}
The projects table has fields id,fields...
The investments table has the fields id,project_id,status,created_at
My issue is that the query runs and returns a collection of the projects which have at least one investment, however the where clause inside the whereHas is ignored, because the resulting collection includes investments with status values different than paid.
Does anyone has any idea of what is going on?
I believe this is what you need
$projects = Project::whereHas('investments', function($q) {
$q->where('status','=','paid');
})->with(['investments' => function($q) {
$q->where('status','=','paid');
}])->get();
whereHas wil check all projects that have paid investments, with will eagerload all those investments.
You're confusing whereHas and with.
The with method will let you load the relationship only if the query returns true.
The whereHas method will let you get only the models which have the relationship which returns true to the query.
So you need to only use with and not mix with with whereHas:
$projects = Project::with(['investments' =>
function($query){ $query->where('status','=','paid'); }])
->get();
Try like this:
$projects = Project::with('investments')->whereHas('investments', function($q) {
$q->where('status','like','paid'); //strings are compared with wildcards.
})
->get();
Change the order. Use with() before the whereHas(). I had a similar problem few weeks ago. Btw, is the only real difference between the problem and the functional example that you made.

Eloquent filter on pivot table created_at

I want to filter the contents of two tables which have an Eloquent belongsToMany() to each other based on the created_at column in the pivot table that joins them. Based on this SO question I came up with the following:
$data = ModelA::with(['ModelB' => function ($q) {
$q->wherePivot('test', '=', 1);
}])->get();
Here I'm using a simple test column to check if it's working, this should be 'created_at'.
What happens though is that I get all the instances of ModelA with the ModelB information if it fits the criteria in the wherePivot(). This makes sense because it's exactly what I'm telling it to do.
My question is how do I limit the results returned based on only the single column in the pivot table? Specifically, I want to get all instances of ModelA and ModelB that were linked after a specific date.
OK, here it goes, since the other answer is still wrong.
First off, wherePivot won't work in whereHas closure. It's BelongsToManys method and works only on the relation object (so it works when eager loading).
$data = ModelA::with(['relation' => function ($q) use ($someDate) {
$q->wherePivot('created_at', '>', $someDate);
// or
// $q->where('pivot_table.created_at', '>', $someDate);
// or if the relation defines withPivot('created_at')
// $q->where('pivot_created_at', '>', $someDate);
}])->whereHas('ModelB', function ($q) use ($someDate) {
// wherePivot won't work here, so:
$q->where('pivot_table.created_at', '>', $someDate);
})->get();
You are using Eager Loading Constraints, which constrain only, like you said, the results of the related table.
What you want to use is whereHas:
$data = ModelA::whereHas('ModelB' => function ($q) {
$q->wherePivot('test', '=', 1);
})->get();
Be aware that ModelB here refers to the name of the relationship.

laravel search many to many Relashionship

I am testing eloquent for the first time and I want to see if it suit my application.
I have Product table:
id, name
and model:
class Produit extends Eloquent {
public function eavs()
{
return $this->belongsToMany('Eav')
->withPivot('value_int', 'value_varchar', 'value_date');
}
}
and eav table:
id, name, code, field_type
and pivot table:
product_id, eav_id, value_int, value_varchar, value_date
class Eav extends Eloquent {
public function produitTypes()
{
return $this->belongsToMany(
'ProduitType'
->withPivot('cs_attributs_produits_types_required');
}
All this is working.
But I want to search in that relashionship:
e.g: all product that have eav_id=3 and value_int=3
I have tested this:
$produits = Produit::with( array('eavs' => function($query)
{
$query->where('id', '3')->where('value_int', '3');
}))->get();
But I get all the product, and eav data only for these who have id=3 and value_int=3.
I want to get only the product that match this search...
Thank you
I know the question is very old. But added the answer that works in the latest versions of Laravel.
In Laravel 6.x+ versions you can use whereHas method.
So your query will look like this:
Produit::whereHas('eavs', function (Builder $query) {
// Query the pivot table
$query->where('eav_id', 3);
})->get()
My suggestion and something I like to follow is to start with what you know. In this case, we know the eav_id, so let's go from there.
$produits = Eav::find(3)->produits()->where('value_int', '3')->get();
Eager loading in this case isn't going to save you any performance because we are cutting down the 1+n query problem as described in the documentation because we are starting off with using find(). It's also going to be a lot easier to read and understand.
Using query builder for checking multiple eavs
$produits = DB::table('produits')
->join('eav_produit', 'eav_produit.produit_id', '=', 'produits.id')
->join('eavs', 'eavs.id', '=', 'eav_produit.eav_id')
->where(function($query)
{
$query->where('eav_produit.value_int','=','3');
$query->where('eavs.id', '=', '3');
})
->orWhere(function($query)
{
$query->where('eav_produit.value_int','=','1');
$query->where('eavs.id', '=', '1');
})
->select('produits.*')
->get();
Making it work with what you already have...
$produits = Produit::with( array('eavs' => function($query)
{
$query->where('id', '3')->where('value_int', '3');
$query->orWhere('id', '1')->where('value_int', '1');
}))->get();
foreach($produits as $produit)
{
if(!produit->eavs)
continue;
// Do stuff
}
From http://four.laravel.com/docs/eloquent:
When accessing the records for a model, you may wish to limit your results based on the existence of a relationship. For example, you wish to pull all blog posts that have at least one comment. To do so, you may use the has method
$posts = Post::has('comments')->get();
Using the "has()" method should give you an array with only products that have EAV that match your criteria.
$produits = Produit::with( array('eavs' => function($query)
{
$query->where('id', '3')->where('value_int', '3');
}))->has('eavs')->get();

Resources