我又在和Lavarel挣扎。我有一个可以将角色应用于用户的表单。但是当我应用一个角色并单击“提交”按钮时,什么也没有发生。似乎未执行对表单定义的操作,但我不知道为什么。
表单的路径位于超级用户处。
这是我的刀片文件:
@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
我的控制器:
<?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();
}
}
}
和我的路线
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']
]);
});
我真的希望你们能帮助我!