0

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.

1
  • it will return value 1 thats not problem Commented Feb 19, 2016 at 10:53

3 Answers 3

4
->where(function($query) use($userid, $user){
    $query->where('suserid' , $userid);
    $query->orwhere('riemailid' , $user); 
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use 'use' keyword in your query.

Also write orwhere query as below:-

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

Hope it will help you :)

Comments

0

make sure variable not write in '' (single quote)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.