V
V
Vladislav2017-03-01 16:56:26
Laravel
Vladislav, 2017-03-01 16:56:26

How to implement rights to users with their laravel roles?

Tell me how you can implement it:
When you enter under the manager, only 2 actions are available to him - adding and deleting.
It seems that I did, but when editing or adding an error in the table with roles

//Таблица ролей
        Schema::create('roles',function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',50);
            $table->timestamps();
        });
//таблица пользователей
Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->unsigned();
            $table->foreign('role_id')->references('id')->on('roles');
            $table->string('name')->nullable();
            $table->string('company_name')->nullable();
            $table->string('email', 100)->unique();
            $table->string('password')->nullable();
            $table->rememberToken();
            $table->boolean('active');
            $table->timestamps();
    });

Mistake :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.role_id' in 'where clause' (SQL: select * from `roles` where `roles`.`role_id` = 1 and `roles`.`role_id` is not null)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav, 2017-03-01
@mzcoding

There is no role_id field in the roles table.
You need to register the relation, in users , something like:

public  function role()
    {
        return $this->hasOne(Role::class, 'user_id', 'id');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question