I'm using laravel 5.2 multi auth so I have created a new management table instead of user table.
I have installed Entrust for roles and permissions. During the entrust installation, I have changed entrust:migration content as:
public function up()
{
// Create table for storing roles
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
// Create table for associating roles to users (Many-to-Many)
Schema::create('role_management', function (Blueprint $table) {
$table->integer('management_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('management_id')->references('id')->on('managements')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['management_id', 'role_id']);
});
// Create table for storing permissions
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
// Create table for associating permissions to roles (Many-to-Many)
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
}
and entrust.php
as:
return [
'role' => 'App\Role',
'roles_table' => 'roles',
'permission' => 'App\Permission',
'role_user_table' => 'role_management',
];
and I have 3 models, Role and Permission and Management. Everything works fine, but the problem is when I want to attachRoles into the role_management
table with code below:
public function store(userManagementFormRequest $request)
{
$user=new Management(array(
'name'=>$request->get('name'),
'password'=>bcrypt($request->get('password')),
));
$user->save();
$user->attachRoles($_POST['role']);
return redirect()->back()->with('userStatus','عملیات با موفقیت انجام گردید.');
}
it will show an error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'digikala.management_role' doesn't exist (SQL: insert into
management_role
(created_at
,management_id
,role_id
,updated_at
) values (2016-03-21 14:26:24, 1, 1, 2016-03-21 14:26:24))
I don't know why it shows management_role
instead of role_management
, while the right table name is role_management
. Also, I have just two columns, management_id
and role_id
, in this table, but also in this error it wants to insert created_at
and updated_at
into this table.
I have used composer config:cache and composer cache:clear and compose dump-autoload but nothing happened.
Is there anything wrong in my code? management model:
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class Management extends Authenticatable
{
use EntrustUserTrait;
protected $table='managements';
protected $fillable=['name','password'];
public function roles()
{
return $this->belongsToMany('App\Role')->withTimestamps();
}
}
There is no need to override the roles
relationship function. It already exists in the EntrustUserTrait
.
You can remove it and it should work.