laravel: use model relationship with conditions

As you know, we usually use model relationships on Laravel. Sometimes, you might need to conditional relationships between models. For example: you have 2 models: Post and Comment. You might want to get approved comments via model relations. In order to do that, you can use this method:

// in Post.php modelpublic function comments(): HasMany{return $this->hasMany(Comment::class);}public function approvedComments(): HasMany{return $this->hasMany(Comment::class)->where('is_approved', true);}

That's it. You can also add more conditions like writing a query.

I hope you find this useful. Happy coding!