[英]why undefined variable in closure laravel?
I have two 3 tables : users,profiles, friend_request 我有两个3表: 用户,个人资料,friend_request
$my_profile_id variable store the value of user's profile ID $ 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
variable is working fine in where
and orwhere
clause but when I use in whereNotIn
sub query clause it create error: Variable is not defined $my_profile_id
变量在where
和orwhere
子句中工作正常,但是当我在whereNotIn
子查询子句中使用时,它会创建错误:未定义变量
While your code style is a little hard to read, It looks like you are missing a step. 虽然您的代码风格有点难以阅读,但看起来您似乎缺少了一步。 However, my preferred code style has already been put down in a reply with probably working code. 但是,我的首选代码风格已被包含在可能的工作代码中。 Don't make live to hard on your self and follow that code style. 不要自欺欺人,不要遵循那种代码风格。
Your code: 您的代码:
whereNotIn('profiles.profile_id', function($q)
{
Posted code: 张贴代码:
whereNotIn('profiles.profile_id', function($q) use ($my_profile_id) {
What happened was you missed a complete instruction on what to use. 发生的事情是您错过了有关使用方法的完整说明。
u create a closure
but did not pass parameter into Closure
so your code is not working u创建一个closure
但没有将参数传递给Closure
因此您的代码无法正常工作
since inside the closure PHP doesn't know the $my_profile_id
variable you need to have a use
statement in the closure too 因为在闭包内部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();
I guess you are not passing $my_profile_id
variable inside closure... That`s why it showing variable is undefined
我猜你没有在闭包内部传递$my_profile_id
变量...这就是为什么它显示variable is undefined
$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();
Try adding use ($my_user_id)
after function($q)
. 尝试在function($q)
之后添加use ($my_user_id)
function($q)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.