mardi 10 octobre 2017

Laravel: has(..)->select(..) does not work on columns of related table

I have a user table like this

id | name 
1  | Bob
2  | Alice

Each user has zero or one related car. This is my car table:

id | user_id | color | type
1  |  2      | red   | electric

In the user model I have set the relation by:

public function car()
{
  return $this->hasOne('App\car');
}

I want to have the name of all users who have a car, and the color of the car that they own. I do not want any other information. I tried:

User::has('car')->select('name','color')->get();

However, this throws the error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'color' in 'field list'

Why can't it find the column color? How can I fix this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire