質問

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