Laravel Eloquent with and find

Why is this not working?

Article::with('category')->find($ids)

I got a Array to String Conversion Exception.

But if i split the query into 2 parts like this:

$articles = Article::with('category')

and

$articles = $articles->find($ids)

I didn't get any exception and the result is right.


Solution 1:

Just for the posterity... other way you can do this is:

Article::with('category')->whereIn('id', $ids)->get();

This should be faster because it's leaving the query to the database manager

Solution 2:

Try:

Article::with('category')->get()->find($ids);

You need to get the articles first before you can call find() I believe.

Warning: This retrieves every single article from the database and loads them all into memory, and then selects just one from all that data and returns it. That is probably not how you would want to handle this problem.

Solution 3:

This will give you the results based on an array of IDs in Laravel 4

Article::whereIn('id', $ids)->with('category')->get();