Saturday, March 4, 2017

Laravel eloquent relationship 'with' keyword

I have a table called rides where I save user_id,and driver_id. so the rides table is id|user_id|driver_id|ticket_id|created_at|updated_at. All users(client,driver) are saved in users table. While retrieving the a row from the rides table I write

$rides = Ride::with('client','driver')->get();

Relationships:

public function client()
{
   return $this->belongsTo(User::class,'user_id');
}

public function driver()
{
   return $this->belongsTo(User::class,'driver_id');
}

Now the problem is when a row is retrieved, Only the information of client comes. driver is null. After swapping the parameters driver and client

$rides = Ride::with('driver','client')->get();

driver information comes, and client becomes null.. How can I solve this problem?



via Noob Coder

Advertisement