尝试删除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',
];

更新到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

尝试替换Config::get('auth.products.users.model')

看起来您可能没有正确设置Entrust配置。

当您尝试删除角色,并且Config::get()调用返回的字符串不是有效的类或对象时,对EntrustRoleTrait中删除事件的method_exists()调用将引发此异常。

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

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

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

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