php - Need help in creating a user in Laravel with roles

I followed a nice tutorial on how to create roles and how to use gates with Laravel.

I can seed users with roles and edit them, but I would like to be able to create a user and give him/her one or more roles and I don't know where to start (I'm not a pro, but I need to finish this app).

Here's all the code I can show you so far :

Users Controller :

public function edit(User $user, $id)
{
    $user = User::findOrFail($id);
    $roles = Role::all();

    return view('admin.users.edit',compact('user', 'roles'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\User  $user
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, User $user, $id)
{
    $user = User::findOrFail($id);
    $user->roles()->sync($request->roles);

    $user->name = $request->name;
    $user->email = $request->email;

    $user->save();

    return redirect()->route('admin.utilisateurs.index');
}

Roles Table :

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

Pivot Table :

public function up()
{
    Schema::create('role_user', function (Blueprint $table) {
        $table->id();
        $table->BigInteger('role_id')->unsigned()->onDelete('cascade');
        $table->BigInteger('user_id')->unsigned()->onDelete('cascade');
        $table->timestamps();
    });
}

Edit blade file with checkboxes :

<div class="block-content">
    <div class="form-group">
        @foreach ($roles as $role)
            <div class="form-group form-check">
                <input type="checkbox" class="form-check-input" name="roles[]"
                    value="{{ $role->id }}" id="{{ $role->id }}" 
                    @if ($user->roles->pluck('id')->contains($role->id)) checked @endif>
                <label class="" for="{{ $role->id }}">{{ $role->name }}</label>
            </div>
        @endforeach
    </div>
</div>

The thing is that I don't really know how to write my code on the Create Blade File.

Here's the create method on the controller (not sure if it's correct or not) :

public function store(Request $request, User $user)
{
    $user = new User();

    $user->roles()->sync($request->roles);

    $user->name = $request->name;
    $user->email = $request->email;

    $user->save();

    return redirect()->route('admin.utilisateurs.index')->with('success','Utilisateur ajout?�');
}

Thanks for reading this long message!

Peace

Answer

Solution:

on create blade it's almost as same as edit blade. you just need not to check for existing roles.

//rest of the form first like user name and email
<div class="block-content">
    <div class="form-group">
        @foreach ($roles as $role)
            <div class="form-group form-check">
                <input type="checkbox" class="form-check-input" name="roles[]" value="{{ $role->id }}" id="{{ $role->id }}">
                <label class="" for="{{ $role->id }}">{{ $role->name }}</label>
            </div>
        @endforeach
    </div>
</div>

and then in controller instead of sync just use attach

public function store(Request $request)
{
    $user = new User();
    $user->name = $request->name;
    $user->email = $request->email;

    $user->save();
    $user->roles()->attach($request->roles);

    return redirect()->route('admin.utilisateurs.index')->with('success','Utilisateurajout?�');
}

look i have removed model binding in store function. it's not necessary in here. and attached roles after saving the user. docs on attach and sync in here.

Source