User model:
public function followers() {
return $this->hasMany('Follower', 'f_who');
}
Profile Controller:
$user_followers = User::with('followers')->find($user_id)->toArray();
Result
Array
(
[user_id] => 1
[username] => dev
[email] => [email protected]
[name] => AdminName
[surname] => AdminSur
[profile_image] => /img/anon1.jpg
[ip] => 127.0.0.1
[pp] => 0
[np] => 0
[followers_count] => 2
[photos_count] => 0
[created_at] => 2014-12-17 12:08:04
[updated_at] => 2015-01-18 21:15:55
[followers] => Array
(
[0] => Array
(
[follower_id] => 1
[f_who] => 1
[f_which] => 2
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
)
[1] => Array
(
[follower_id] => 2
[f_who] => 1
[f_which] => 3
[created_at] => -0001-11-30 00:00:00
[updated_at] => -0001-11-30 00:00:00
)
)
)
I need to get followers names,surnames,etc (left join users table) in the child "followers" array using relationships. I'm really confused with this relationships. If the question or structure is unclear, tell me i'll try to fix that.
f_who - means the person who is followed.
f_which - the person who is following
if I try belongsToMany relationship like this:
public function followers() {
return $this->belongsToMany('Follower', 'users')->withPivot('f_who');
}
I get:
Unknown column 'users.follower_id' in 'field list' (SQL: select `followers`.*, `users`.`user_id` as `pivot_user_id`, `users`.`follower_id` as `pivot_follower_id`, `users`.`f_who` as `pivot_f_who` from `followers` inner join `users` on `followers`.`follower_id` = `users`.`follower_id` where `users`.`user_id` in (1))
It should select users.user_id`
followerstable is a pivot table handling a many-to-many relationship between your users, correct? In that case, you need to use a BelongsToMany relationship. laravel.com/docs/4.2/eloquent#working-with-pivot-tables - TonyArra