Frage

I currently have a simple situation which I'm trying to find a good solution for - in my app, one Post has zero-to-many comments. I'm trying to work out how to query with either Fluent or Eloquent to find only the posts which have comments attached.

At the moment, the best I've been able to come up with is

Post::with('comments')->whereNotNull('comment.id')->get();

which fails because just doing with('comments') doesn't actually run a join on that table.

I've tried Googling and looking at some other SO posts, but haven't been able to find anything useful - is there some simple solution I'm completely missing out on?

Cheers.

War es hilfreich?

Lösung

To expand on Anams response, you can do any of the following

Post::has('comments')->get();
Post::has('comments', '>', 0)->get();
Post::has('comments', '>=', 1)->get();

For more information on querying relations, check out the laravel documentation http://laravel.com/docs/eloquent#querying-relations.

Andere Tipps

Try the following:

Post::has('comments')->get();

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top