Basic Laravel 9 Course is available now! 🎉 Get your seat now

Laravel Model 101 Tips

#laravel #tips

Just a quick Laravel tips and tricks

Laravel 101 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 orderBy
    public function productsByName() {
        return $this->hasMany(Product::class)->orderBy('name')
  • Everything should work as normal, please share this posts If it's helpful to you.