我是新手,但我目前正在尝试建立单向好友列表关系,如您所见,我只获得用户的用户 ID,但我需要他们的用户名,问题是,这些 ID 来自关系表“朋友”作为查询的结果,它没有用户名,而是存储 users_id。
这就是我得到的!
这是我的配置
*usuarios stands for users*
table usuarios:
id->primary
username
friends table:
id->primary
user_id->reference->users.id
friend_id->reference->users.id
用户型号:
public function friends()
{
return $this->belongsToMany('App\User', 'friends', 'user_id', 'friend_id');
}
现在的路线:
/* get single user by id */
Route::get('user/{usuario}', function($usuario) {
$usuario = DB::table('usuarios')->where('username', $usuario)->first();
$friends = DB::table('friends')->where('user_id', '=', $usuario->id)->get();
return view('users.profile')
->with('friends', $friends)
->with('user', $usuario);
});
模板
<ul style="list-style-type: none">
<li><a href="#">friends placeholder</a>
@foreach ($friends as $friend)
<li> {{$friend->friend_id}}</li>
@endforeach
</li>
</ul>
说着,我想见见我朋友的朋友。
非常感谢!告诉我我是否遗漏了什么。