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

时间:2016-01-08 14:30:46

标签: php laravel laravel-5 eloquent laravel-5.2

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

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

我正在访问的网址是:

/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',

];

3 个答案:

答案 0 :(得分:4)

更新到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.providers.users.model')上替换 Entrust / Traits / EntrustRoleTrait.php配置:: get('auth.model')

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分:0)

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

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