2016-02-19 33 views
0

这是我的SQL查询PHP laravel未定义的变量:用户ID在控制器

select * from users us join EmailBox em on us.userid = em.suserid 
where em.status = "Delete" and (em.suserid="$userid" or em.riemailid="$user") 

我要检查任何一个领域,如果只有“suserid”或“riemailid”的结果。 所以我改变了这个laravel querybuilder。

$lists = EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id') 
       ->where('status' , 'Delete') 
       ->where(function($query){ 
       $query->where('suserid' , '$userid'); 
       $query->orwhere('riemailid' , '$user');  
     })->paginate(5); 

此查询工作正常时,该值将被直接传递, 用户ID直接传递1表示

$lists = EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id') 
        ->where('status' , 'Delete') 
        ->where(function($query){ 
        $query->where('suserid' , 1); 
        $query->orwhere('riemailid' , '$user'); 
     })->paginate(5); 

这是我在laravel代码。 什么是错误?

if(Auth::check()){ 
     //if(Auth::user()->email!="") 
     $user=Auth::user()->email; 
     $userid=Auth::user()->id; 
     $lists = EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id') 
          ->where('status' , 'Delete') 
          ->where(function($query){ 
           $query->where('suserid' , '$userid'); 
           $query->orwhere('riemailid' , '$user'); 
          })->paginate(5); 
     $links = $lists->render(); 
     return view('front.index', compact('lists', 'links')); 
    } 

我得到错误$ userid变量是未定义的。

+0

它会返回值1多数民众赞成没有问题 –

回答

4
->where(function($query) use($userid, $user){ 
    $query->where('suserid' , $userid); 
    $query->orwhere('riemailid' , $user); 
} 
0

您需要在查询中使用'use'关键字。

还写orwhere查询如下: -

EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id') 
      ->where('status' , 'Delete') 
      ->where(function($query) use ($userid, $user){ 
       $query->where('suserid' , $userid) 
        ->orwhere('riemailid' , $user); 
      })->paginate(5); 

希望它会帮助你:)

0

确保变量不是写在 ''(单引号)

if(Auth::check()){ 
     //if(Auth::user()->email!="") 
     $user=Auth::user()->email; 
     $userid=Auth::user()->id; 
     $lists = EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id') 
          ->where('status' , 'Delete') 
          ->where(function($query){ 
           $query->where('suserid' , $userid); 
           $query->orwhere('riemailid' , $user); 
          })->paginate(5); 
     $links = $lists->render(); 
     return view('front.index', compact('lists', 'links')); 
    }