Following ACL in Laravel : Roles and permissions laracast, I am facing a big issue. I even tried on a new project with a 5.1 version and it's still not working as you can see below.
What am I doing wrong?
Here's my files, if someone can help me.
Here's what I do in tinker :
Psy Shell v0.7.2 (PHP 5.6.23 — cli) by Justin Hileman9m
>>> namespace App;
=> null
>>> $role = new Role;
=> App\Role {#736}
>>> $perm = new Permission;
=> App\Permission {#738}
>>> $role->where('name','admin')->first();
=> App\Role {#752
id: 1,
name: "admin",
label: "Administrateur",
created_at: "2016-09-02 15:56:02",
updated_at: "2016-09-02 15:56:02",
}
>>> $perm->where('name','users_edit')->first();
=> App\Permission {#753
id: 1,
name: "users_edit",
label: "Edition des utilisateurs",
created_at: "2016-09-02 15:56:02",
updated_at: "2016-09-02 15:56:02",
}
>>> $role->givePermissionTo($perm);
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `chr53_permissions`
(`updated_at`, `created_at`) values (2016-09-02 16:15:13, 2016-09-02 16:15:13))'
Here's my Role.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
//
protected $table = 'roles';
protected $fillable = [
'name', 'label',
];
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
public function users() {
return $this->belongsToMany(User::class);
}
public function givePermissionTo(Permission $permission)
{
return $this->permissions()->save($permission);
}
}
Here's my Permission.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Permission extends Model
{
//
protected $table = 'permissions';
protected $fillable = [
'name', 'label',
];
public function roles() {
return $this->belongsToMany(Roles::class);
}
}
Here's my migration file :
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class RolesPermissionInit extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
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')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('permission_role', function($table) {
$table->dropForeign(['permission_id']);
$table->dropForeign(['role_id']);
$table->drop();
});
Schema::table('role_user', function($table) {
$table->dropForeign(['role_id']);
$table->dropForeign(['user_id']);
$table->drop();
});
Schema::drop('roles');
Schema::drop('permissions');
}
}