1

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
  • Why not use whereIn Post::whereIn('slug', $slugs)->get(); Commented May 28, 2019 at 20:38
  • Please add more informations and piece of code or pseudo code what you're actually trying to achieve. Commented May 28, 2019 at 20:40

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

If am getting this right? You need to fetch your posts to an array called $slugs. Simply:

$slugs = Posts::all()->toArray();

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.