我有在MySQL中正常工作的sql查询。我想使用与用户表的联接从配置文件表中获取配置文件ID。
SELECT profiles.profile_id FROM profiles
left JOIN users on profiles.id = users.id where users.id = 1
Laravel查询构建器查询为
$my_user_id = Auth::user()->id;
$my_id = DB::table('profiles')->select('profiles.profile_id')
->leftjoin('users','users.id' ,'=' ,'profiles.id')
->pluck('profiles.profile_id')
->where('users.id',$my_user_id)
->first();
答案 0 :(得分:0)
您还应该选择ID来执行加入:
DB::table('profiles')->select('profiles.profile_id', 'profiles.id')
->leftjoin('users','users.id' ,'=' ,'profiles.id')
->where('users.id',$my_user_id)
->pluck('profiles.profile_id')
->first();
答案 1 :(得分:0)
在where()
之前致电pluck()
(不需要select()
):
$my_id = DB::table('profiles')
->leftJoin('users', 'users.id', '=' , 'profiles.id')
->where('users.id', $my_user_id)
->pluck('profiles.profile_id')
->first();
您也可以使用value()
来缩短它:
$my_id = DB::table('profiles')
->leftJoin('users', 'users.id', '=' , 'profiles.id')
->where('users.id', $my_user_id)
->value('profiles.profile_id');