Laravel 5.7. I have a model Cat
, with a property visible
, and a scope to check for visibility:
class Cat extends Model
{
public function scopeVisible($query)
{
return $query->where('visible', true);
}
public function fleas()
{
return $this->hasMany('App\Flea');
}
}
I have a second model, Flea
:
class Flea extends Model
{
public function cat()
{
return $this->belongsTo('App\Cat');
}
}
I want to add a similar visibility scope to Flea
, so that I would be able to do something like $fleas = Flea::visible()->get()
, which would only return Fleas which belong to visible Cats. But I'm not sure how I can reference the parent Cat
in the scope method on Flea
.
Edit: I try to access the cat()
relationship in a scopeVisible
method on Flea
, but I get this error from Laravel:
Property [cat] does not exist on the Eloquent builder instance.