Implementation error

Asked

Viewed 36 times

2

I’m calling the class TenantScope in the model Cliente and makes a mistake

Tenantscope.php

namespace App\Scopes\Tenant;

use App\Tenant\ManagerTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Query\Builder;

class TenantScope implements Scope

{
    public function apply(Builder $builder, Model $model)
    {


        $tenant = new ManagerTenant;
        $tenant->getTenantIdentify();

        $builder->where('tenant_id', $tenant)->get();
    }
}

Error:

Whoops\Exception\ErrorException  : Declaration of App\Scopes\Tenant\TenantScope::apply(Illuminate\Database\Query\Builder $builder, Illuminate\Database\Eloquent\Model $model) must be compatible with Illuminate\Database\Eloquent\Scope::apply(Illuminate\Database\Eloquent\Builder $builder, Illuminate\Database\Eloquent\Model $model)

Client.php //Model

<?php

namespace App\Models;

use app\Scopes\Tenant\TenantScope;
use Illuminate\Database\Eloquent\Model;

class Cliente extends Model
{
    protected $fillable = [
        'nome',
     ];

    ´    public static function boot()
    {
        parent::boot();

        static::addGlobalScope(new TenantScope);
    }
}

1 answer

2


According to Exception itself, the prototype of its function apply is accepting a type parameter Illuminate\Database\Query\Builder, when you should accept one of those Illuminate\Database\Eloquent\Builder.

In this case, simply fix the Builder class import:

Tenantscope.php

use Illuminate\Database\Query\Builder; // antes
use Illuminate\Database\Eloquent\Builder; // depois
  • Perfect, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.