php - How can i get First table records on base of third table by using laravel 5 eloquent model -
requested tables listed below:
**user table** id, name 1, vehicle person name 2, renter person name **vehicle table** id, user_id, vehicle_name 1, 1, car **booking table** id, renter_id, vehicle_id 1, 2, 1
user model
public function renter() { return $this->hasmany(booking::class, 'renter_id'); } public function vehiclebook() { return $this->hasmanythrough(booking::class, vehicle::class); }
booking model
public function user() { return $this->belongsto(user::class, 'renter_id'); }
vehicle model
public function user() { return $this->belongsto(user::class); }
my controller
$renters = auth::user()->renter()->get(); $owners = auth::user()->vehiclebook()->get(); // in loop $renter->user->name; // renter person name $owner->user->name; // vehicle person name
result
on base of booking table want renter person , vehicle person name using laravel 5 orm.
i have done using 2 calls want know if there way result using 1 call ?
you this. reduce number of lines, increase number of queries.
$user = user::find(auth::user()->id)->with('renters')->with('vehiclebooks')->first();
Comments
Post a Comment