It seems like you're trying to create a global scope that applies a condition through a nested relationship. The error you're encountering suggests that you're trying to call a method on a collection that doesn't exist. This is likely because you're trying to use the getRelated method on a collection result instead of on the relationship itself.
To define a global scope that involves nested relationships, you should define the scope within a class that implements the Scope interface and then apply it to your model. Here's an example of how you can define and apply a global scope that filters models based on a nested relationship:
First, define the global scope:
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class ProvidersViaCoursesScope implements Scope
{
protected $relationName1;
protected $relationName2;
protected $relationAttributeToDisplay2;
protected $arrayOfData;
public function __construct($relationName1, $relationName2, $relationAttributeToDisplay2, $arrayOfData)
{
$this->relationName1 = $relationName1;
$this->relationName2 = $relationName2;
$this->relationAttributeToDisplay2 = $relationAttributeToDisplay2;
$this->arrayOfData = $arrayOfData;
}
public function apply(Builder $builder, Model $model)
{
$builder->whereHas($this->relationName1, function ($query) {
$query->whereHas($this->relationName2, function ($query) {
$query->whereIn($this->relationAttributeToDisplay2, $this->arrayOfData);
});
});
}
}
Then, apply the scope to your model:
class YourModel extends Model
{
protected static function booted()
{
static::addGlobalScope(new ProvidersViaCoursesScope(
'relationName1', 'relationName2', 'relationAttributeToDisplay2', ['array', 'of', 'data']
));
}
}
Replace 'relationName1', 'relationName2', 'relationAttributeToDisplay2', and ['array', 'of', 'data'] with the actual names and values you want to use for your relationships and conditions.
This way, the global scope will be applied to all queries for YourModel, and it will filter the results based on the conditions defined through the nested relationships.