Query relationship Eloquent

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Handling Query Relationships with Eloquent for Trashed Comments Body:

Query relationships play a crucial role in Laravel applications. They help maintain database connections between different models. In our case, we have news and comments, where the news model has many comments. But our problem is that these comments may be trashed, and we want to fetch only those which are not trashed (trashed != 1). The question arises whether there's a way to use Eloquent relationships to get around this issue or should we go with a different approach.

To begin with, let us first understand the concepts involved. The News model has a one-to-many relationship with comments and uses the hasMany() method to establish it. The issue here is that we don't want all comments from this many-to-one relationship but only those which are not trashed. This can be solved in three different ways:

1. Using Eloquent Relationships:
$news = News::find(123);
   $news->comments->where('trashed', '<>', 1); //Pseudo-code for a potential solution with relationships
Unfortunately, the pseudo-code above is not currently possible in Laravel. However, the good news is that Eloquent does support eager loading of related models. Let's see how we can use it to optimize our query:
$news = News::find(123)->with(['comments' => function ($query) { $query->where('trashed', '<>', 1); }])->get();
In this example, we use the with() method to specify eager loading of comments. Within it, we provide a closure containing the conditions for the comment query. The result is that only untrashed comments will be loaded alongside the news object. While efficient and cleaner than manual queries, this approach might still lead to issues if you have a large number of records or if your application needs to filter on multiple conditions. 2. Using Static Eloquent Methods:
$news = News::find(123);
   $comments = Comment::where('trashed', '<>', 1)
       ->where('news_id', '=', $news->id)
       ->get();
This is a more explicit approach, where we first fetch the news object and then create a separate query for comments based on the conditions required. This allows us to apply multiple filters if needed and provides better control over the data being returned. However, it might involve making two database queries, one for the news and another for the comment query. 3. Using Query Scopes: Query scopes are a powerful way of combining common search conditions into methods that can be called on your models. You can define a scope for trashed comments as follows:
/** @mixin Model */
trait UntrashedScope {
   public function scopeUntrashed($query) {
       return $query->where('trashed', '<>', 1);
   }
}
Now we can apply this scope to our comment model:
$news = News::find(123);
$comments = Comment::untrashed()
    ->where('news_id', '=', $news->id)
    ->get();
This approach allows us to define a search scope and apply it as needed. We can also chain other conditions or join relationships, making the query even more refined. In conclusion, there are multiple ways to handle the situation of fetching comments related to news without trashing: using eager loading with Eloquent relationships, static Eloquent methods, or query scopes. Each approach has its pros and cons. To choose the best one for your specific needs, consider factors such as performance, scalability, and application requirements. Whichever method you opt for, remember that Laravel's powerful ORM tools can help make your queries faster and more efficient.