【问题标题】:why undefined variable in closure laravel?为什么闭包 laravel 中有未定义的变量?
【发布时间】:2019-04-17 10:44:05
【问题描述】:

我有两个 3 表:users,profiles,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 变量在 whereorwhere 子句中工作正常,但是当我在 whereNotIn 子查询子句中使用时,它会产生错误:未定义变量

【问题讨论】:

    标签: mysql laravel laravel-5 query-builder laravel-query-builder


    【解决方案1】:

    虽然您的代码风格有点难以阅读,但您似乎错过了一步。但是,我喜欢的代码风格已经在回复中包含可能工作的代码。不要过分苛求自己,并遵循这种代码风格。

    你的代码:

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

    发布代码:

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

    发生的情况是您错过了有关使用什么的完整说明。

    【讨论】:

      【解决方案2】:

      你创建了一个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(); 
      

      更多关于Closuresee的信息

      【讨论】:

      • 它的工作,你能告诉我为什么我的代码没有使用就不能工作($my_profile_id)
      • @Entrepreuner ya 我用澄清更新了我的答案
      【解决方案3】:

      我猜你没有在闭包内传递$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();
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-03
        • 2012-10-20
        • 2021-11-12
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 2017-07-19
        相关资源
        最近更新 更多