how to use IN operator in laravel query (eleqouent and raw query)?

I need to use the IN operator to get the data from the database. I tried using it as below and got an error:

$pr =DB::('select * from prstuff p where p.pid in (select pid from prdrop)');

I am new to Laravel and don’t know exactly how to use the operators like IN, so please explain to me how to use it.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you can set the custom select in DB::raw() like this :

DB::select(DB::raw('select * from prstuff p where p.pid in (select pid from prdrop)'));

or you can use whereIn() like this:
DB::table('prstuff')
->select('*')
->whereIn('pid', function($query)
{
    $query->select('pid')
    ->from('prdrop');
})
->get();

Method 2

You are not calling any funtion on your db class. You can call the select function like this DB::select ()

$pr =DB::select('select * from prstuff p where p.pid in (select pid from prdrop)');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x