Я создал глобальную область видимости, следуя документации Laravel 5.4 для Eloquent Laravel => красноречивые => queryScopes
Имя области, которое я определил, это «TenantScope» Имя файла «TenantScope.php» в папке «app \ Scopes»
<?php
namespace app\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class TenantScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param Illuminate\Database\EloquentBuilder $builder
* @param Illuminate\Database\EloquentModel $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->where('tenant_id',1);
}
}
Модель выглядит так:
<?php
namespace app\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Log;
use app\Scopes\TenantScope ;
class Vehicle extends Model
{
use SoftDeletes;
public $table = 'vehicles';
protected $dates = ['deleted_at'];public $fillable = [
'name',
'vin',
'type'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'name' => 'required',
'vin' => 'required'
];
protected static function boot()
{
parent::boot();
Log::info('booting vehicle model');
static::addGlobalScope(new TenantScope);
}
Когда я запускаю приложение, я получаю «500 Internal Server Error», и журналы laravel показывают следующее:
[2017-03-10 11:17:49] local.INFO: booting vehicle model
[2017-03-10 11:17:49] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'app\Scopes\TenantScope' not found in /var/www/html/fleetio/app/Models/Vehicle.php:65
Vehicle.php: 65: static::addGlobalScope(new TenantScope);
Трассировки стека :
#0 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(295): app\Models\Vehicle::boot()
#1 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(276): Illuminate\Database\Eloquent\Model->bootIfNotBooted()
#2 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(858): Illuminate\Database\Eloquent\Model->__construct()
#3 /var/www/html/fleetio/app/User.php(32): Illuminate\Database\Eloquent\Model->hasMany('\\App\\Models\\Veh...')
#4 /var/www/html/fleetio/app/Http/Controllers/API/VehicleAPIController.php(24): App\User->vehicles()
#5 [internal function]: app\Http\Controllers\API\VehicleAPIController->index(Object(App\Http\Requests\APIRequest))
#6 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)
#7 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction('index', Array)
#8 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Route.php(189): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(app\Http\Controllers\API\VehicleAPIController), 'index')
#9 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Route.php(144): Illuminate\Routing\Route->runController()
#10 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Router.php(653): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#11 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 /var/www/html/fleetio/app/Http/Middleware/JWTAuthenticated.php(81): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#13 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): App\Http\Middleware\JWTAuthenticated->handle(Object(Illuminate\Http\Request), Object(Closure))
#14 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#16 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#17 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#18 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#19 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), '60', '1')
#20 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#22 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Router.php(655): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#23 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Router.php(629): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#24 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Router.php(607): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#25 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(268): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#26 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#27 /var/www/html/fleetio/app/Http/Middleware/cors.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#28 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): App\Http\Middleware\cors->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#31 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#34 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(150): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#35 /var/www/html/fleetio/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(117): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#36 /var/www/html/fleetio/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#37 {main}
Я вроде как тяну свои волосы на этом сейчас. Как я не могу понять, что пошло не так.
Ну, думаю, вы должны бежать
composer dump-autoload
Когда вы создаете класс, вам нужно запустить эту команду для его автозагрузки.
Других решений пока нет …