I have an array called $slugs and I need to receive data form database table called posts in single array how to archive that ?
2 Answers
If I'm not wrong you have array of slugs like this ['slug', 'slug1', 'slug2'] and you want to get values from table where slugs matches your slugs array in a single query.
//Solution via eloquent
$slugs = ['slug', 'slug1', 'slug2'];
Post::whereIn('slug', $slugs)->get();
// Solution via Query builder
use Illuminate\Support\Facades\DB;
$slugs = ['slug', 'slug1', 'slug2'];
DB::table('posts')->whereIn('slug', $slugs)->get();
If you want to convert all your posts response to an array and then assign it to $slugs array you should use below solution
$posts = Post::all()->toArray();
$slugs[] = $posts;
To know more about whereIn visit
whereInPost::whereIn('slug', $slugs)->get();