Laravel Model 101 Tips
Just a quick Laravel tips and tricks

Last Updated 14 May 2022
#2 Conditional Relationships
We can split where
condition to a separate method relationship.
public function comments(){ return $this->hasMany(Comment::class);} public function approved_commments() { return $this->hasMany(Comment::class)->where('approved',1);}
#1 OrderBy on Eloquent Relationships
public function products() { return $this->hasMany(Product::class);} // use orderBypublic function productsByName() { return $this->hasMany(Product::class)->orderBy('name')}
- Everything should work as normal, please share this posts If it's helpful to you.