尝试删除 laravel 5.2 中的模型时抛出异常

Exception being thrown when trying to delete model in laravel 5.2

我最近将一个 Laravel 5.1 应用程序升级到了 5.2。这之前工作正常,但自从升级后我遇到了问题。当我尝试删除我的一个模型时,出现以下异常:

FatalErrorException in Model.php line 1011:
Class name must be a valid object or a string

我访问的URL是:

/admin/roles/delete/4

它的路线是:

Route::get('admin/roles/delete/{id}', ['as' => 'admin.roles.delete', 'uses' => 'Admin\RolesController@destroy']);

控制器代码为:

public function destroy($id)
{
    $role = Role::find($id);
    $role->delete();

    Session::flash('message', '<div class="alert alert-success" role="alert">The role has been deleted.</div>');

    return redirect(route('admin.roles'));
}

型号编码为:

<?php namespace App;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{
    protected $fillable = ['name', 'display_name', 'description'];
}

可能值得一提的是我正在使用 Entrust 包。这是我的委托配置文件的内容:

<?php

/**
 * This file is part of Entrust,
 * a role & permission management solution for Laravel.
 *
 * @license MIT
 * @package Zizaco\Entrust
 */

return [

    /*
    |--------------------------------------------------------------------------
    | Entrust Role Model
    |--------------------------------------------------------------------------
    |
    | This is the Role model used by Entrust to create correct relations.  Update
    | the role if it is in a different namespace.
    |
    */
    'role' => 'App\Role',

    /*
    |--------------------------------------------------------------------------
    | Entrust Roles Table
    |--------------------------------------------------------------------------
    |
    | This is the roles table used by Entrust to save roles to the database.
    |
    */
    'roles_table' => 'roles',

    /*
    |--------------------------------------------------------------------------
    | Entrust Permission Model
    |--------------------------------------------------------------------------
    |
    | This is the Permission model used by Entrust to create correct relations.
    | Update the permission if it is in a different namespace.
    |
    */
    'permission' => 'App\Permission',

    /*
    |--------------------------------------------------------------------------
    | Entrust Permissions Table
    |--------------------------------------------------------------------------
    |
    | This is the permissions table used by Entrust to save permissions to the
    | database.
    |
    */
    'permissions_table' => 'permissions',

    /*
    |--------------------------------------------------------------------------
    | Entrust permission_role Table
    |--------------------------------------------------------------------------
    |
    | This is the permission_role table used by Entrust to save relationship
    | between permissions and roles to the database.
    |
    */
    'permission_role_table' => 'permission_role',

    /*
    |--------------------------------------------------------------------------
    | Entrust role_user Table
    |--------------------------------------------------------------------------
    |
    | This is the role_user table used by Entrust to save assigned roles to the
    | database.
    |
    */
    'role_user_table' => 'role_user',

];

看来您的 Entrust 配置设置不正确。

当您尝试删除角色并且 Config::get() 调用返回的字符串不是不是有效的 class 或对象。

https://github.com/Zizaco/entrust/blob/master/src/Entrust/Traits/EntrustRoleTrait.php#L75

检查您的配置并确保您已正确设置模型名称。

更新到 5.2 后,您更改了 config/auth。php

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

https://github.com/Zizaco/entrust/blob/master/src/Entrust/Traits/EntrustRoleTrait.php#L48

尝试在 [=26 上替换 Entrust/Traits/EntrustRoleTrait.php Config::get('auth.model') =]('auth.providers.users.model')

看起来 Entrust 还不完全兼容 Laravel 5.2。我已经在 Github 上提出了这个问题,其他人也有同样的问题。

https://github.com/Zizaco/entrust/issues/472