我有两个表:用户,概要文件,friend_request
$my_profile_id变量存储用户配置文件ID的值
$my_user_id = Auth::user()->id;
$my_profile_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();
$friend_accept = DB::table('friend_request')->select('profiles.profile_id','profiles.first_name','interest_request.sent_at','interest_request.interest_id')->leftjoin('profiles', function($join){
$join->on('interest_request.sender_id' , '=','profiles.profile_id');
$join->orOn('interest_request.to_user_id' , '=','profiles.profile_id');
})->where('to_user_id',$my_profile_id)->orwhere('sender_id',$my_profile_id)->where('interest_status','2')->whereNotIn('profiles.profile_id', function($q)
{
$q->select('profiles.profile_id')->from('profiles')->where('profiles.profile_id',$my_profile_id);
})->groupby('profiles.profile_id')->get(); $my_profile_id变量在where和orwhere子句中运行良好,但是当我在whereNotIn子查询子句中使用时,它会创建错误:变量未定义
发布于 2018-11-14 12:55:04
创建一个closure,但没有将参数传递到Closure,因此代码无法工作。
由于在闭包中,PHP不知道$my_profile_id变量,所以闭包中也需要一个use语句
$my_user_id = Auth::user()->id;
$my_profile_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();
$friend_accept = DB::table('friend_request')->select('profiles.profile_id','profiles.first_name','interest_request.sent_at','interest_request.interest_id')->leftjoin('profiles', function($join){
$join->on('interest_request.sender_id' , '=','profiles.profile_id');
$join->orOn('interest_request.to_user_id' , '=','profiles.profile_id');
})->where('to_user_id',$my_profile_id)
->orwhere('sender_id',$my_profile_id)
->where('interest_status','2')
->whereNotIn('profiles.profile_id', function($q) use ($my_profile_id) {
$q->select('profiles.profile_id')->from('profiles')
->where('profiles.profile_id',$my_profile_id);
})->groupby('profiles.profile_id')->get(); 有关Closure see的更多信息
发布于 2018-11-14 13:01:43
虽然您的代码样式有点难读,但您似乎遗漏了一步。但是,我喜欢的代码样式已经在一个可能正在工作的代码的回复中被记录下来了。不要自欺欺人,要遵循这种代码风格。
你的代码:
whereNotIn('profiles.profile_id', function($q)
{张贴代码:
whereNotIn('profiles.profile_id', function($q) use ($my_profile_id) {所发生的是你错过了一个关于使用什么的完整说明。
发布于 2018-11-14 12:59:25
我想您不会在闭包中传递$my_profile_id变量..。s why it showing变量是未定义的
$my_user_id = Auth::user()->id;
$my_profile_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();
$friend_accept = DB::table('friend_request')->select('profiles.profile_id','profiles.first_name','interest_request.sent_at','interest_request.interest_id')->leftjoin('profiles', function($join){
$join->on('interest_request.sender_id' , '=','profiles.profile_id');
$join->orOn('interest_request.to_user_id' , '=','profiles.profile_id');
})->where('to_user_id',$my_profile_id)->orwhere('sender_id',$my_profile_id)->where('interest_status','2')->whereNotIn('profiles.profile_id', function($q) use ($my_user_id){ $q->select('profiles.profile_id')->from('profiles')->where('profiles.profile_id',$my_profile_id); })->groupby('profiles.profile_id')->get();尝试在use ($my_user_id)之后添加function($q)。
https://stackoverflow.com/questions/53300674
复制相似问题