Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lifesound's avatar

Elequent relationship query

I have Item that hasMany statuses ..

I want to create a method inside the item model to know if it has a status 'Review' or not I came up with this but no hope.

    public function scopeReviewed($query)
    {
        return $this->whereHas('statuses',function(Builder $query){
            $query->where('status','Review');
        })->count() > 0;
    }

any help?

0 likes
1 reply
ehab.aboshehab's avatar

You can use getAttribute laravel magic function in your model :

public function getIsScopeReviewedAttribute()
    {
        return $this->whereHas('statuses', function($query){
            $query->where('status', 'Review');
        })->exists();
    }

Then you can call it like this :

$model->is_scope_reviewed;

Please or to participate in this conversation.