문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

Try the following:

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top