0

I'm struggling with Lavarel (again). I have a form on which I can apply roles to the users. But when i apply a role and click the submit button, nothing happens. It seems as if the action that is defined to the form is not executed, but I can't figure out why.

The path to the form is at superuser/users.

This is my blade file:

@section('content')
    <div class="container">
            <h1>Gebruikerspermissies aanpassen</h1>
            <table class='table'>
                <thead>
                <th>Voornaam</th>
                <th>Achternaam</th>
                <th>E-mail</th>
                <th>User</th>
                <th>Admin</th>
                <th>Superuser</th>
                </thead>
                <tbody>
                    @foreach($users as $user)
                        <tr>
                        <form action="{{ route('superuser.assign') }}" method="post">
                            @csrf
                            <td>{{ $user->first_name }}</td>
                            <td>{{ $user->name }}</td>
                            <td><input type="hidden" name="email" value="{{$user->email}}" />{{ $user->email }}</td>
                            <td><input type="checkbox" name="role_user" {{ $user->hasRole('User') ? 'checked' : '' }}></td>
                            <td><input type="checkbox" name="role_admin" {{ $user->hasRole('Admin') ? 'checked' : '' }}></td>
                            <td><input type="checkbox" name="role_superuser" {{ $user->hasRole('Superuser') ? 'checked' : '' }}></td>
                            <td><button type="submit" value="Submit">Opslaan</button></td>
                        </form>
                        </tr>
                    @endforeach
                </tbody>
            </table>


        </div>
    @endsection

My controller:

<?php
namespace App\Http\Controllers;
use App\User;
use App\Role;
use Illuminate\Http\Request;
class UserController extends Controller
{


    public function userList()
    {
        $users = User::all();
        return view('superuser', ['users' => $users]);
    }

    public function postAssignRoles(Request $request)
    {
       $user = User::where('email', $request['email'])->first();

       $user->roles()->detach();
       if ($request['role_user'])
       {
           $user->roles()->attach(Role::where('name', 'User')->first());
       }

       if ($request['role_admin'])
       {
           $user->roles()->attach(Role::where('name', 'Admin')->first());
       }

       if ($request['role_superuser'])
       {
           $user->roles()->attach(Role::where('name', 'Superuser')->first());
       }

       if($request->method() == 'POST'){
        return redirect()->back();
       }
    }
}

and my routes

Route::group(['middleware' => 'web'], function () {
    Route::get('/', function () {
        return view('welcome');
    })->name('main');

    Auth::routes();

    Route::get('/home', 'HomeController@index')->name('home');

    Route::get('/admin', function () {
        return view('admin');
    });

    Route::get('/superuser', [
        'as' => 'superuser',
        'middleware' => 'roles',
        'roles' => ['Superuser'],
        function() {return view('superuser');}
    ]);

    Route::get('/superuser/users', [
        'uses' => 'UserController@userList',
        'as' => 'superuser.users',
        'middleware' => 'roles',
        'roles' => ['Superuser']
    ]);

    Route::post('superuser/users/assign-roles', [
        'uses' => 'UserController@postAssignRoles',
        'as' => 'superuser.assign',
        'middleware' => 'roles',
        'roles' => ['Superuser']
    ]);

    Route::get('/superuser/clubs', [
        'uses' => 'ClubController@clubList',
        'as' => 'superuser.clubs',
        'middleware' => 'roles',
        'roles' => ['Superuser']
    ]);

    Route::post('/superuser/clubs/add-club', [
        'uses' => 'ClubController@postAddClub',
        'as' => 'superuser.addclub',
        'middleware' => 'roles',
        'roles' => ['Superuser']
    ]);

});

I really hope you guys can help me out!

Yorg Vermeulen
  • 206
  • 3
  • 15
  • 1
    Your markup isn't valid. `
    ` does not belong as a direct child of ``
    – Phil Mar 29 '19 at 02:33
  • Possible duplicate of [HTML: Is it possible to have a FORM tag in each TABLE ROW in a XHTML valid way?](https://stackoverflow.com/questions/1249688/html-is-it-possible-to-have-a-form-tag-in-each-table-row-in-a-xhtml-valid-way). Make sure you read all the answers, not just the accepted one – Phil Mar 29 '19 at 02:35
  • Alright, didn't know that. Got it to work now, thanks! – Yorg Vermeulen Mar 29 '19 at 03:23

0 Answers0