Laravel 5.4 应用于模型启动的全局范围导致 Class "xxxx" 在 [模型启动方法] 中找不到

Laravel 5.4 Global scopes applied to Model boot results in Class "xxxx" not found in [Model boot method]

我已经根据 Laravel 5.4 Eloquent Laravel=>eloquent=>queryScopes

的文档创建了一个全局范围

我定义的作用域名称是"TenantScope" "app\Scopes" 文件夹

中的文件名 "TenantScope.php"
<?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 内部服务器错误”并且 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

当您创建一个 class 时,您需要 运行 这个命令来自动加载它。