Only one whereHas is working in deep eloquent relationship - laravel

I have a deep eloquent relation.
What I’m trying to do is filter the actions.treatmentActions.treatment.date with $date_range and actions.treatmentActions.paid which equal 1. The date_range filter is working fine, but the paid filter isn’t. The collection is still including the actions.treatmentActions.paid that equal to 0
Any thought on this? am I missing something?
$polis = Poli::with('actions')
->whereHas('actions.treatmentActions.treatment', function ($treatment) use ($date_range){
$treatment->whereBetween('date', $date_range);
})
->whereHas('actions.treatmentActions', function ($treatmentActions){
$treatmentActions->where('paid', 1);
})
->where('id', '!=', 4)
->get();
I've tried different approach like this, but still not working:
$polis = Poli::with('actions')
->whereHas('actions', function ($actions) use ($date_range){
$actions->whereHas('treatmentActions', function ($treatmentActions) use ($date_range){
$treatmentActions->whereHas('treatment', function ($treatment) use ($date_range){
$treatment->where('paid', 1)->whereBetween('date', $date_range);
});
});
})
->where('id', '!=', 4)
->get();

Fixed it with callback in with
$polis = Poli::with(['actions.treatmentActions' => function($query){
$query->where('paid', 1);
}])
->whereHas('actions.treatmentActions.treatment', function ($treatment) use ($date_range){
$treatment->whereBetween('date', $date_range);
})
->get();
Anyone can link me an article explaining this?

Related

WhereNotIn inside WhereHas

why whereNotIn doesn't work inside whereHas?
Model::whereHas('statuses', function ($query) {
$query->whereNotIn('title',['X,'Y','Z']);
})
I posted as answer workarounds but can someone explain it or it's BUG?
Laravel 6
Some workarounds:
->whereDoesntHave('statuses', function ($query) {
$query->whereIn('title',['X','Y','Z']);
})
or
->whereHas('statuses', function ($query) {
$query->whereIn('title', ['X,'Y','Z']);
$query->having(\DB::raw("COUNT(table_statuses.id)"), '=', 0);
})
It's not a bug, it only works if the relationship is one to one if it one to many you have to use whereDoesnHave

Laravel: Use order by in combination with whereHas

I want to retrieve a collection of data, which is ordered by the start_date of the relation
Basically I want to achieve this, with Laravel Models (the code below works perfectly)
$posts = DB::table('posts')
->leftJoin(
'threads',
'posts.id',
'=',
'threads.postable_id'
)
->where('threads.postable_type', '=', 'App\Post')
->orderBy('threads.start_date')
->paginate($request->input('limit', 2));
So in this case, I'm fetching ALL Posts and those are ordered by the start_date of the thread relation.
Those are not my actual tables but this works perfectly!
Because I'm using https://laravel.com/docs/8.x/eloquent-resources this is not the ideal solution to retrieve sorted data.
So instead I want to use the orderBy clause somewhere here
$posts = Post::whereHas('thread', function ($query) {
$query->where('end_date', '>=', Carbon::now());
});
But I just cannot make this work. I've tried this
$posts = Post::whereHas('thread', function ($query) {
$query->where('end_date', '>=', Carbon::now())
->orderBy('start_date');
});
and I also appended this to the actual relation:
public function thread(): MorphOne
{
return $this->morphOne('App\Thread', 'postable')->orderBy('start_date');
}
If you look at your code:
$posts = Post::whereHas('thread', function ($query) {
$query->where('end_date', '>=', Carbon::now())
->orderBy('start_date');
});
the whereHas will only return Post associate with a thread which the function return true.
Try this:
$posts = Post::with('thread')->has('thread')->orderBy('thread.start_date')->get();
This will fetch all Post with Thread only if they have at least one Thread and then orderBy the start_date of the Thread.
You don't have to do the whereHas function because when you call ->with('thread') it'll use you this :
public function thread(): MorphOne
{
return $this->morphOne('App\Thread', 'postable')->orderBy('start_date');
}
whereHas doesnt retrieve the relationship.
If you need even more power, you may use the whereHas and orWhereHas methods to define additional query constraints on your has queries, such as inspecting the content of a comment: Laravel whereHas
Don't do :
$posts = Post::with('thread')->orderBy('thread.start_date');
If there is no thread on some post, post without thread will be fetch with value null for their key thread and you will have an unexpected result when you try to orderBy.
First of all I want to thank Elie Morin for his help but I found out that I definitely need to use joins for that task.
In my example, I wanted to order the main query (posts) by the relation's start_date
Doing what you suggested
$posts = Post::with('thread')->has('thread')->orderBy('thread.start_date')->get();
Would only order the thread by start_date and not the ENTIRE query.
Which is why I came up with something like this:
$posts = Post::has('thread')
->select('posts.id')
->leftJoin(
'thread',
'posts.id',
'=',
'thread.postable_id'
)
->where('thread.postable_type', '=', 'App\Post')
->where('thread.end_date', '>=', Carbon::now())
->orderBy('thread.start_date')
->with('thread');
return PostResource::collection($posts->paginate(2));

Add Parameter to Laravel Eloquent relationship in "with" function

I would like to add a parameter to my Eloquent relationship in the with:: static method.
I have a Dealer Model that has those relationships
public function events()
{
return $this->hasMany('App\Event', 'dealer_id', 'dealer_id');
}
public function recentEvents($interval = 14)
{
return $this->events()
->whereDate('datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('dateend', '>=', Carbon::now('America/Toronto'));
}
Everything works fine if I call it like this $dealer->recentEvents(20) but in my DealerController
I would like to do something in the line of this
Dealer::with("recentEvents")
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();
Where i would pass a paremeter to "recentEvents".
I've tried putting a closure function to with like this
Dealer::with(["recentEvents" => function ($query) use ($interval){
//The problem here is that I would need to put the recent-events code like this
$query->whereDate('cal_datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('cal_dateend', '>=', Carbon::now('America/Toronto'));
}])
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();
But it would defeat the purpose of having a recentEvents relationship.
Is it possible to just pass a parameter ?
I've tried using Dynamic Scope from Laravel doc but I figured it was not what I was looking for.
i think u should use like this
Dealer::with(["events",function($q) use($interval){
$q->whereDate('datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('dateend', '>=', Carbon::now('America/Toronto'));
}])
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();

laravel - send a selected field to with function

how can i use "airpot_id" in with function?
City::select([
"cities.id",
"airports.id As airport_id"
])
->RightJoin('airports', function ($join)
{
$join->on('cities.id', '=', 'airports.city_id');
})
->with(["airports" => function ($q)
{
$q->where("id",airport_id) //I want to use here.
}])
->paginate(env("PER_PAGE", 10))->toArray();
Why are you using joints like this? Use the eloquent query builder. Maybe you should post you modal, relations and your objective. In general, if we need to pass value inside function we use use($variable). Something like this
$airport_id = 1;
$data = City::with(["airports" => function ($q) use($airport_id){ //Like this we pass data
$q->where("id",airport_id)
}])->get();
I don't think this will work in your case

Getting a relationship with a condition?

I have category and article tables, they are in a many to many relationship. i have the relationships set up on the models.
How can I get all articles where a category id is = 1?
I've tried:
Article::whereHas('category', function ($query) use ($id) {
$query->where('category_id', $id);
})->get();
The above works, but seems clunky, is there a more efficient way in eloquent to do this?
You can use eager loading technique.
Article::with(['category', function ($query) use ($id) {
$query->where('category_id', $id);
}])->get();
The following groupBy code is assuming that Category::find($id)->articles is not an option for whatever reason.
What about groupBy?
Article::groupBy('category')
->having('category', $id)
->get();
whereHas will return articles that match that condition, but won’t filter articles then fetched by the same conditions. You could therefore create an anonymous function that contains the constraints, and pass it to both the whereHas and with methods:
$filter = function ($query) use ($categoryId) {
$query->where('category_id', '=', $categoryId);
};
Article::whereHas('category', $filter)
->with(['category' => $filter])
->get();

Resources