2016-09-27 86 views
1

我正在使用依賴關係Zizaco/entrust在我的web應用程序中創建角色及其權限,該應用程序構建在laravel 5.3中。我遇到的問題是(正如標題所說),只要我嘗試保存新創建的權限。它存儲在roles表中。Zizaco/entrust保存權限使用角色表

遷移文件尚未從原始zizaco/entrust包更改。我使用php artisan vendor:publish來創建遷移。角色和權限的車型還是比較喜歡的文件說:

namespace App\Models; 

use Zizaco\Entrust\EntrustRole; 

class Role extends EntrustRole 
{ 

protected $table = 'roles'; 

/** 
* The attributes that are mass assignable. 
* 
* @var array 
*/ 
    protected $fillable = [ 
     'name', 'display_name', 'description', 
    ]; 
} 

和上述許可

namespace App\Models; 

use Zizaco\Entrust\EntrustRole; 

class Permission extends EntrustRole 
{ 

protected $table = 'permissions'; 

/** 
* The attributes that are mass assignable. 
* 
* @var array 
*/ 
protected $fillable = [ 
     'name', 'display_name', 'description', 
    ]; 
} 

,並在播種機文件,我用下面的代碼來生成的作用,播種機:

use App\Models\Permission; 
use App\Models\Role; 
use Illuminate\Database\Seeder; 

class RoleSeeder extends Seeder 
{ 
/** 
* Run the database seeds. 
* 
* @return void 
*/ 
public function run() 
{ 
    $owner = new Role(); 
    $owner->name   = 'owner'; 
    $owner->display_name = 'Eigenaar'; 
    $owner->save(); 

    $createOwner = new Permission(); 
    $createOwner->name   = 'create-owner'; 
    $createOwner->display_name = 'Eigenaar toevoegen'; 
    $createOwner->save(); 
} 

委託的配置文件也被修改爲角色和權限模型以及表的正確路徑。

我已經嘗試了作曲家dump-autoload甚至php工匠緩存:清除。

有什麼,我錯過了?請幫忙。 如果語法不正確,請原諒我的英語。

編輯:當我嘗試通過以下行來我的允許附加到角色:

$owner->attachPermission($createOwner); 

我得到一個SQL錯誤:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fail s (reunions_dev . permission_role , CONSTRAINT permission_role_permission_id_foreign FOREIGN KEY (permission_id) REFERENCES permissions (id) ON DELETE CASCADE ON UPDATE CASCADE)

是因爲沒有權限的記錄將ID鏈接到。有點明顯。

回答

0

The problem I am having is (as the title says) whenever I try to save a newly created Permission. It is stored in the roles table.

看着你提供的代碼,你擴展了你的Permission模型的錯誤類。在權限模型變更用途的聲明&擴展類EntrustPermission

use Zizaco\Entrust\EntrustPermission; 

class Permission extends EntrustPermission 
+0

三江源採取瞄了一眼我的代碼。我無法相信我以前沒有看到它!我現在正在接受另一個錯誤。每當我嘗試在播種機類中初始化它時,我都會收到錯誤:'必需的參數$ auth missing'你知道我該如何解決這個問題嗎? – Hoffie