How can I show only values ​with all values in whereIn?

Viewed 48

Now it will show post_id with tags 85 or 86 or 87.

How can I show only post_id with tags 85 and 86 and 87?

                           $values = DB::table('post_tag')
                                ->whereIn('tag_id', [85, 86, 87])
                                ->pluck('post_id');

This is the table structure

enter image description here

4 Answers

You can do something like this below

$values = DB::table('post_tag')
             ->where('tag_id', 85)
             ->where('tag_id', 86)
             ->where('tag_id', 87)
             ->pluck('post_id');

Add toArray() at the end of your query

$values = DB::table('post_tag')->whereIn('tag_id', [85, 86, 87])->pluck('post_id')->toArray();

you should add groupBy('post_id')

$values = DB::table('post_tag')->whereIn('tag_id', [85, 86, 87])->groupBy('post_id')->pluck('post_id')->toArray();

How can I show only values ​with all values in whereIn()? It is working fine in my project.

$users = User::whereIn('id',[12,13,14])->pluck('id')->toArray();
dd($users);
   

Output

array:3 [▼
  0 => 12
  1 => 13
  2 => 14
]
Related