jeudi 28 juillet 2016

How to get First table records on base of third table by laravel 5 eloquent model

I have three tables

**User Table**
id, name
1, vehicle person name
2, renter person name

**Vehicle Table**
id, user_id, vehicle_name
1, 1, My 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

$renter = Auth::user()->renter()->get();
$owner = Auth::user()->vehicleBook()->get();

$renter->user->name; // renter person name
$owner->user->name; // vehicle person name

Result

On base of booking Table i want to get renter person and vehicle person name using Laravel 5 ORM.

I have done that using two calls but i want to know if there is any way to get result using one call ?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2aAapyb
via IFTTT

Aucun commentaire:

Enregistrer un commentaire