为什么在closure laravel中没有定义变量?

xesrikrc  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(295)

我有两个3表:用户,配置文件,朋友请求
$my\u profile\u 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` 子查询子句it create error:未定义变量
vtwuwzda

vtwuwzda1#

虽然您的代码样式有点难以阅读,但看起来您缺少了一个步骤。然而,我喜欢的代码风格已经在回复中写下了,很可能是可以工作的代码。不要让生活对你自己太苛刻,要遵循那种代码风格。
您的代码:

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

发布代码:

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

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

fdx2calv

fdx2calv2#

创建一个 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 看见

ne5o7dgx

ne5o7dgx3#

我想你不会通过的 $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();

尝试添加 `use ($my_user_id)` 之后 `function($q)` .

相关问题