laravel - In Larave 3l, I want to get results using Eloquent from a has_many relationship -
i have 3 tables:
invites, nests , users.
invites have nest_id. nests have user_id
i want users have same nest_id. how do in elequent?
the models have has_many relationship
i trying in controller:
$users = user::with('invite')->where('invites.nest_id', '=', $id)->get();
i getting error:
sqlstate[42s22]: column not found: 1054 unknown column 'invites.nest_id' in 'where clause' sql: select * `users` `invites`.`nest_id` = ? bindings: array ( 0 => '2', ) it saying not have nest_id column, do.
class user extends eloquent { public static $table = 'users'; public static $accessible = array('username', 'email', 'picture', 'password'); public function nest() { return $this->has_many('nest'); } public function invite() { return $this->has_many('invite'); } class nest extends eloquent { public static $table = 'nests'; public function user() { return $this->belongs_to('user'); } public function idea() { return $this->has_many('idea'); } public function invite() { return $this->has_many_and_belongs_to('invite'); }
laravel relationships not use joins, other tables in relationship not accessible. relationships involving "many" works out more efficient. mean have think things backwards - need users of invites have given nest.
$invites = invite::with('user')->where('nest_id', '=', $nest_id); foreach ($invites $invite) { $invite->user; }
Comments
Post a Comment