I have two tables that has One to Many relationship.
Bookings - (id)
booking_tasks - (id, booking_id,user_id)
one booking has many task one task has one booking
Booking Model:
public function tasks() {
return $this->hasMany('App\BookingTask');
}
BookingTask Model
public function booking() {
return $this->belongsTo('App\Booking');
}
I want to get list of bookings that, user_id = 2 for latest booking_task for the bookings. I do not want to check other old booking_tasks of the bookings.
ex:
I want to check whether the last record of the booking_task's user_id=2 then get it as a booking. In the example last booking_task's user_id = 5. So it will not get as booking.
My code is:
$bookings=Booking::whereHas('tasks',function($q){
$q->where('user_id',2);//this will check whether any of record has user_id =2
})->get();
I used this also: But it is not a correct one,
$bookings=Booking::whereHas('tasks',function($q){
$q->latest()->where('user_id',2)->limit(1);//this will check whether any of record has user_id =2 and return latest one.
})->get();
Ho can I solve this problem, I have to use Laravel Eloquent also.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire