53

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.

Marco
  • 1,579
  • 1
  • 19
  • 34
  • 2
    Can you paste the stack trace of the "array to string conversion" exception? As far as I can remember what you first posted should work. **Edit:** Make sure `$ids` is a single ID and not an array of IDs. If you want to find articles where the ID is in an array use `where_in('id', $ids)->get()`. – Jason Lewis Jun 01 '13 at 01:20
  • This explained it for me: https://stackoverflow.com/questions/34617150/php-laravel-how-to-eager-load-find-method/34617322#comment95851422_34617322 – Ryan Feb 04 '19 at 21:22

4 Answers4

50

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

Jim U
  • 3,318
  • 1
  • 14
  • 24
dani24
  • 2,108
  • 2
  • 26
  • 28
  • 1
    The other answer submitted by Adam that was already posted three months earlier does exactly the same thing. The order of the "with" and "whereIn" calls doesn't make any difference. Both are equally good answers. – orrd Jan 06 '17 at 00:34
  • 1
    Thanks Ord, I did wonder why mine wasn't accepted. Even easier in L5 now. Article::with('category')->find($ids); – Adam Feb 02 '17 at 17:16
  • 2
    I'll rather use ``->first();`` instead of ``->get();``. The first one will return the expected article while the second one will return a collection. – enbermudas Dec 08 '18 at 18:23
  • If you see the question, he wanted to get the `$articles` where the id belongs to `$ids`. He didn't want to get a single Article – dani24 Dec 09 '18 at 20:27
  • I get an error `Allowed memory size of 268435456 bytes exhausted` – Noah Gary Mar 13 '19 at 14:18
  • @NoahThomasGary How many ids do you have? The Model that you're loading has a lot of data? – dani24 Mar 14 '19 at 18:57
23

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.

orrd
  • 9,469
  • 5
  • 39
  • 31
Rob W
  • 9,134
  • 1
  • 30
  • 50
  • No, this is not working. I didn't get any exception, but eloquent only use one id. And the result from your solution is null. Any other ideas? – Marco May 31 '13 at 19:21
  • Does `var_dump(Article::with('category')->first())` yield anything? – Rob W May 31 '13 at 21:38
  • 1
    Yes. This is correctly working and also this works right: `Article::with('category')->find(5)`. But if i use find with an array i got an Array to String Exception. I am confused. – Marco Jun 01 '13 at 13:13
  • For this to work you need to define function "category()" in the "app/models/Article.php" . Look here for help http://laravel.com/docs/4.2/eloquent#relationships – Ryu_hayabusa Feb 18 '15 at 16:18
  • 3
    No, don't do it that way! That would retrieve every single article from the database (!) and then just return the one that matches "$ids" (which by the way, should be called "$id" because it has to be a single value, not an array!). See the accepted answer instead. – orrd Jan 06 '17 at 00:23
10

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

Article::whereIn('id', $ids)->with('category')->get();
Adam
  • 356
  • 4
  • 14
-2

Create object and set his properties with relationships

for example, code in your Controller or Service

$article = new Article;
$article = $article->find($id);

$result->article = $article;
$result->article->category = $article->category;

return response()->json($result);

Code in your Model

public function category() {
    return $this->hasOne('App\Category');
}
S1lllver
  • 191
  • 7