Mysql 为什么在闭包laravel中未定义变量?

Mysql 为什么在闭包laravel中未定义变量?,mysql,laravel,laravel-5,query-builder,laravel-query-builder,Mysql,Laravel,Laravel 5,Query Builder,Laravel Query Builder,我有两个3表:用户、个人资料、好友请求 $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',$

我有两个3表:用户、个人资料、好友请求

$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\u id
变量在
where
或where
子句中工作正常,但当我在
whereNotIn
子查询子句中使用时,它会创建错误:变量未定义u创建
闭包
,但未将参数传递到
闭包
,因此代码不工作

由于在闭包中PHP不知道
$my\u profile\u 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

的更多信息,我猜您没有在Closure中传递
$my\u profile\u 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) use ($my_user_id){ $q->select('profiles.profile_id')->from('profiles')->where('profiles.profile_id',$my_profile_id); })->groupby('profiles.profile_id')->get();

尝试在
函数($q)
之后添加
use($my\u user\id)
,虽然您的代码样式有点难以阅读,但看起来您缺少了一个步骤。然而,我喜欢的代码风格已经在一封回复中写下了,可能是工作代码。不要让你的生活变得艰难,要遵循代码风格

您的代码:

whereNotIn('profiles.profile_id', function($q)
{
张贴代码:

whereNotIn('profiles.profile_id', function($q) use ($my_profile_id) {

发生的事情是你错过了一个完整的使用说明

它可以工作了,你能告诉我为什么我的代码没有使用就不能工作($my_profile_id)@enterpreuner ya我更新了我的答案并进行了澄清