I'm having trouble understanding how eager loading works with Lauravel/Eloquent. I'd like to grab all clients, do some stuff with them, and output their brand_name
. I have these tables:
Clients
+-------------------+
| Field |
+-------------------+
| client_id |
| client_name |
| client_brand_id |
+-------------------+
Brands
+-------------------+
| Field |
+-------------------+
| brand_id |
| brand_name |
+-------------------+
In the client model I have the relationship:
public function brand()
{
return $this->hasOne('Brand', 'client_brand_id', 'brand_id');
}
And inverse in the brands model:
public function clients()
{
return $this->hasMany('Client', 'brand_id', 'client_brand_id');
}
I want to do this, but it doesn't work:
foreach( Client::with( 'brand' )->get( array('client_id', 'client_name' ) ) as $client ){
echo $client->brand->brand_name;
}