Only until Jan 16th: coupon RESOLUTION25 for 40% off Yearly/Lifetime membership!

Read more here

Conditional relationships

If you notice that you use same relationship often with additional "where" or "orderBy" condition, you can create a separate relationship method.

Model:

// Model:
public function comments()
{
return $this->hasMany(Comment::class);
}
 
public function approvedComments()
{
return $this->comments()
->where('approved', 1);
}
 
public function latestApprovedComments()
{
return $this->comments()
->where('approved', 1)
->orderBy('created_at', 'desc');
}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 69 courses (1205 lessons, total 45 h 02 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses