I'm making a project where I need to create a user as an admin. But when I submit the form it won't post to the database and just redirect to the page. I think there is something wrong with my UserController.php
but I can't find the problem.
This is my code:
UserController.php:
public function createUser(Request $request)
{
$data = request()->validate([
'firstname' => 'required|string|max:255',
'lastname' => 'required|string|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|string|min:8|confirmed',
'role_id' => 'required',
],[
'firstname.required' => 'Voornaam niet ingevuld of te veel tekens.',
'lastname.required' => 'Achternaam niet ingevuld of te veel tekens.',
'email.required' => 'Email bestaat al of is niet ingevuld.',
'password.required' => 'Wachtwoord moet ingevuld zijn, heeft niet minimaal 8 tekens of is niet hetzelfde ingevuld.',
'role_id.required' => 'Rol moet geselecteerd zijn.',
]);
user::create($data);
return redirect::to('/gebruikeraanmaken')->withSucces('Gebruiker is aangemaakt!');
}
User Model:
<?php
namespace App;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name', 'last_name', 'email', 'password', 'role_id',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
createuser.blade.php:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Gebruiker aanmaken') }}</div>
<div class="card-body">
<form action="/gebruikeropslaan" method="post">
@csrf
<div class="form-group row">
<label for="first_name"
class="col-md-4 col-form-label text-md-right">{{ __('Voornaam') }}</label>
<div class="col-md-6">
<input id="first_name" type="text"
class="form-control @error('first_name') is-invalid @enderror"
name="first_name" value="{{ old('first_name') }}" required
autocomplete="first_name" autofocus>
@error('first_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group row">
<label for="last_name"
class="col-md-4 col-form-label text-md-right">{{ __('Achternaam') }}</label>
<div class="col-md-6">
<input id="last_name" type="text"
class="form-control @error('last_name') is-invalid @enderror"
name="last_name" value="{{ old('last_name') }}" required
autocomplete="last_"
autofocus>
@error('last_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email"
class="col-md-4 col-form-label text-md-right">{{ __('Email adres') }}</label>
<div class="col-md-6">
<input id="email" type="email"
class="form-control @error('email') is-invalid @enderror" name="email"
value="{{ old('email') }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="role"
class="col-md-4 col-form-label text-md-right">{{__('Rol')}}</label>
<div class="col-md-6">
<select id="role" class="form-control @error('role') is-invalid @enderror"
name="role" value="{{old('role')}}" required autocomplete="role"
autofocus>
@foreach($roles as $role)
<option value="{{$role->role_id}}">
{{$role->role_name}}
</option>
@endforeach
@error('role')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</select>
</div>
</div>
<div class="form-group row">
<label for="password"
class="col-md-4 col-form-label text-md-right">{{ __('Wachtwoord') }}</label>
<div class="col-md-6">
<input id="password" type="password"
class="form-control @error('password') is-invalid @enderror"
name="password"
required autocomplete="new-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm"
class="col-md-4 col-form-label text-md-right">{{ __('Herhaal wachtwoord') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control"
name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Gebruiker aanmaken') }}
</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
Web.php:
<?php
use IlluminateSupportFacadesRoute;
use AppHttpMiddlewareAdminIsValid;
use IlluminateSupportFacadesAuth;
use AppHttpControllersUserController;
use AppHttpControllersCustomerController;
use AppHttpControllersTicketController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//Welcome page
Route::view('/', 'welcome');
//Admin only routes
Route::middleware(AdminIsValid::class)->group(function () {
Route::get('/gebruikeraanmaken', 'UserController@viewCreateUser');
Route::post('/gebruikeropslaan', 'UserController@createUser');
Route::get('/allegebruikers', [UserController::class, 'allUsers']);
});
Thank you in advance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…