Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jordano1's avatar

User roles

All I've done is laravel new and make:auth. I have spent a lot of time trying to understand how to get this functional, but I'm getting no where. I am building a site for an educational group that needs to manage their assets, they would like, I guess 3 levels of access?

  1. user (registers to the site, can read and download files associated to whatever class they're taking from the professor)

  2. instructors (users can become instructors by sending an email to the administrator, and having the admin give the access to the user. Instructors can post and update only their own content)

  3. admin (has the ability to set users role and delete their accounts)

I've looked around some and noticed a few things, what if the admin gets deleted? Do you just create a new one on the back end?

I have 2 controllers, a User controller from make:auth and I created a Role controller so I can set up the relationship between the two, which I followed along here: https://laravel.com/docs/5.5/eloquent-relationships and read roles are generally belongsToMany. In both my controllers I have the relationship set as belongsToMany.

I have 3 migrations, users, password resets, and roles. I watched this video: https://laracasts.com/series/laravel-from-scratch-2017/episodes/30 and Jeffery talks about a pivot table, I went ahead and created the pivot table inside of my roles table, which is just integer('user_id');, integer('role_id');, and a primary(['user_id', 'role_id']);

I'm basically just trying to build some simple functionality at the moment, like creating a new user with an admin role, but I don't even know how to do that, my migrations worked and all my tables are showing, but I'm not 100% sure what to do next, how do I create a user with the admin role? My roles table looks like this:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->timestamps();
        });

        Schema::create('user_roles', function (Blueprint $table) {
            $table->integer('user_id');
            $table->integer('role_id');
            $table->primary(['user_id', 'role_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('roles');
        Schema::dropIfExists('user_roles');
    }
}

Question 1: I'm using the latest Laravel 5.5.3, and I saw another video where Jeffery just did php artisan generate:pivot or something like that, is there a command still you can write for pivot tables or do you just generally create a table with 2 ints holding the id's of what you want associated, and a primary key for both?

Question 2: do I need to have the 'user', 'instructor', admin' roles as things in my database? All I have in there right now is that it has to be a unique name: $table->string('name')->unique();

Question 3: for uploading files and content, does laravel have some kind of plugin you can use to mass upload files? The group I'm making this site for has lots of audio video and ppt files, I wonder is there a simple solution to going about uploading content to the site?

Question 4: If I have a user who uploads an audio file or video file, those things take a ton of space, how do you efficiently store things like that? Is there a way?

Thanks for any help!

0 likes
3 replies
mushood's avatar

Question 1: I think you are looking for this: https://github.com/laracasts/Laravel-5-Generators-Extended

Question 2: Why not go for a package to manager your roles: https://github.com/spatie/laravel-permission

Question 3: What do you mean by mass upload? If you have a lot of those files already, you can just upload them to your server directly by FTP. Try filezilla

Question 4: Convert your audio file and video file to a smaller sized format before upload or upload to YouTube for example and then embed/link to the video/audio

jordano1's avatar

Hey mushood, thanks for responding.

Question2: I want to understand it so if I run into issues I can fix them. I have the pivot table and I'm sure I wrote everything fine I just wanted to get a simple implementation working, but if this is faster that works for me.

Question3: I mean that for my users with instructor roles being able to upload their own content and I was just wondering if there was a solution for that I could use or if I needed to create some kind of form that asks users to upload content. The idea is to have Instructors being able to upload files like videos and audio so students can download them for classes and stuff. This would be for people who wouldn't have FTP access, but can upload and update their own content. Does that make sense?

Stratos's avatar

In your User model:

/**
     * The roles that belong to the user.
     */
    public function roles()
    {
        return $this->belongsToMany('App\Role')->withTimestamps();
    }

In your Role model:

/**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany('App\User')->withTimestamps();
    }

Migrations: You can do anything with your user migration, it does not matter.

create_roles_table

public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('slug')->unique();
            $table->timestamps();
        });
    }

create_role_user_table

    public function up()
    {
        Schema::create('role_user', function (Blueprint $table) {
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('role_id');
            $table->timestamps();

            $table->unique(['user_id', 'role_id']);
            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('role_id')->references('id')->on('roles');
        });
    }

Retrieving a user roles:

Auth::user()->roles()->get();

Check if your user has certain role, for example role 3 (admin)

if (Auth::user()->roles()->wherePivot('role_id', 3)->first()) {
        // does have role 3
}

Create a user and automatically attach the user role to it

$user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        $user->roles()->attach(1);

Perhaps you should read these: https://laravel.com/docs/5.5/eloquent-relationships#many-to-many https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships

Please or to participate in this conversation.