What is the cleanest way for me to query the next 1 item in a laravel relationship?
I have 2 models, Users and actions (1 user has many actions).
Actions have to have a date, so when someone searches for a date, I want the search to ONLY query the next action date, and no others.
So for example, if I have 3 actions on these dates
25/04/2019
26/05/2019
30/06/2019
and someone searches for actions after 01/05/2019
, it will return NULL
because the next available action isn't before that date?
Here's my code so far
User Action Relationship
public function nextAction(){
return $this->hasOne('App\Models\Actions', 'user_id', 'id')
->where('complete', 0)
->orderBy('action_date', 'ASC')->limit(1);
}
Get next Query
$users = $users->whereHas('nextAction',function($userDateFrom){
$userDateFrom->where('user_actions.action_date', '>=', Carbon::createFromFormat('d/m/Y', request()->filters['datefrom'])->format('Y-m-d H:i:s'));
});
Because the user has actions greater than 01/05/2019
, it is returning the user.
How can I only return the user if the NEXT 1 action (order by action_date asc) meets the parameter?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire