2016-02-19 46 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')); 
    }