Exception being thrown when trying to delete model

2019-08-08 21:56发布

I have recently upgraded a Laravel 5.1 app to 5.2. This was working fine before but since the upgrade I am experiencing an issue. When I try to delete one of my models I get the following exception:

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

The URL I am visiting is:

/admin/roles/delete/4

The route for it is:

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

The controller code is:

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'));
}

The model code is:

<?php namespace App;

use Zizaco\Entrust\EntrustRole;

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

It's probably worth mentioning I'm using the Entrust package. This is the content of my entrust config file:

<?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条回答
2楼-- · 2019-08-08 22:17

After updating to 5.2 you change the 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

try to replace Entrust/Traits/EntrustRoleTrait.php Config::get('auth.model') on Config::get('auth.providers.users.model')

查看更多
Ridiculous、
3楼-- · 2019-08-08 22:19

Looks like Entrust just isn't fully compatible with Laravel 5.2 yet. I've raised this issue on Github and others are having the same problem.

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

查看更多
别忘想泡老子
4楼-- · 2019-08-08 22:37

Looks like you may not have your Entrust configuration set up properly.

The method_exists() call on the deleting event in the EntrustRoleTrait would be throwing this exception when you're trying to delete the role and the returned string from the Config::get() call isn't a valid class or object.

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

Check your configuration and make sure you've set the model name correctly.

查看更多
登录 后发表回答