DNB5brims
DNB5brims

Reputation: 30568

How to use Eloquent ORM to query relationship item with where conditions?

For example, I have a User has many Comments, but I would like to query the Comment is status 1 only. How can I do that using Eloquent ORM in Laravel? Thanks

Upvotes: 0

Views: 574

Answers (1)

Aloys
Aloys

Reputation: 682

Use something like

$user->comments()->where('status', 1)->get();

By adding the parenthesis after comments you will receive a QueryBuilder instance instead of a Collection of results.

Upvotes: 3

Related Questions