Balaguru Murugan Balaguru Murugan - 7 days ago 6
PHP Question

PHP laravel Undefined variable: userid in controllers

This is my sql query

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


I want to check any one field if "suserid" or "riemailid" result only.
So I changed this in 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);


This query is working fine when the value will be directly passed,
userid directly passed 1 means

$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);


This is my code in laravel.
what is mistake?

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'));
}


I got error $userid variable is undefined.

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