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

woxene's avatar

Laravel 5.6 register doesn't work, no redirect and no errors (edited register fields)

For my project I was required to change the default php artisan make:auth User database + registration form. I've got everything ready, but something seems off.

Validation works. Because I do get errors for this. But when I pass the Validation function nothing seems to happen. I just get a refresh where I keep all fields with value="{{ old('name') }}" and nothing gets added to the database nor do I get an error message.

Here are some of my files:

Web.php(routes):

<?php
Auth::routes();

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

//    CRUD CONTROLLERS
Route::resource('users','UserController');

register.blade.php:

<div class="row">
<div class="col card hoverable s12">
    <div class="card-content">
        <form method="POST" action="{{ route('register') }}" aria-label="{{ __('Register') }}">
            @csrf
            <span class="card-title">Add Employee<a class="waves-effect waves-light hoverable btn white-text right" href="{{ route('users.index') }}"><i
                            class="material-icons">arrow_back</i></a></span>
            <div class="row">
                <div class="input-field col s4">
                    <label for="name">Name</label>
                    <input id="name" class="{{ $errors->has('name') ? ' invalid' : '' }}" type="text" value="{{ old('name') }}" name="name" required>
                    @if ($errors->has('name'))
                        <script>M.toast({html: '{{ $errors->first('name') }}'})</script>
                    @endif
                </div>
                <div class="input-field col s4">
                    <label for="lastname">Last Name</label>
                    <input id="lastname" class="{{ $errors->has('lastname') ? ' invalid' : '' }}" type="text" value="{{ old('lastname') }}" name="lastname" required>
                    @if ($errors->has('lastname'))
                        <script>M.toast({html: '{{ $errors->first('lastname') }}'})</script>
                    @endif
                </div>
                <div class="input-field col s4">
                    <label for="initials">Initials</label>
                    <input id="initials" class="{{ $errors->has('initials') ? ' invalid' : '' }}" type="text" value="{{ old('initials') }}" name="initials" required>
                    @if ($errors->has('initials'))
                        <script>M.toast({html: '{{ $errors->first('initials') }}'})</script>
                    @endif
                </div>
            </div>
            <div class="row">
                <div class="input-field col s4">
                    <label for="email">E-Mail</label>
                    <input id="email" class="{{ $errors->has('email') ? ' invalid' : '' }}" type="email" value="{{ old('email') }}" name="email" required>
                    @if ($errors->has('email'))
                        <script>M.toast({html: '{{ $errors->first('email') }}'})</script>
                    @endif
                </div>
                <div class="input-field col s4">
                    <label for="password">Password</label>
                    <input id="password" class="{{ $errors->has('password') ? ' invalid' : '' }}" type="password" value="{{ old('password') }}" name="password" required>
                    @if ($errors->has('password'))
                        <script>M.toast({html: '{{ $errors->first('password') }}'})</script>
                    @endif
                </div>
                <div class="input-field col s4">
                    <label for="password_confirmation">Confirm Password</label>
                    <input id="password_confirmation" class="{{ $errors->has('password_confirmation') ? ' invalid' : '' }}" type="password" name="password_confirmation"
                           required>
                    @if ($errors->has('password_confirmation'))
                        <script>M.toast({html: '{{ $errors->first('password_confirmation') }}'})</script>
                    @endif
                </div>
            </div>
            <div class="row">
                <div class="input-field col s6">
                    <label for="mobile_number">Mobile Number</label>
                    <input id="mobile_number" class="{{ $errors->has('mobile_number') ? ' invalid' : '' }}" type="number" value="{{ old('mobile_number') }}" name="mobile_number"
                           required>
                    @if ($errors->has('mobile_number'))
                        <script>M.toast({html: '{{ $errors->first('mobile_number') }}'})</script>
                    @endif
                </div>
                <div class="input-field col s6">
                    <label for="emergency_number">Emergency Number</label>
                    <input id="emergency_number" class="{{ $errors->has('emergency_number') ? ' invalid' : '' }}" type="number" value="{{ old('emergency_number') }}" name="emergency_number"
                           required>
                    @if ($errors->has('emergency_number'))
                        <script>M.toast({html: '{{ $errors->first('emergency_number') }}'})</script>
                    @endif
                </div>
            </div>
            <div class="row">
                <div class="input-field col s6">
                    <label for="date_of_birth">Date of Birth</label>
                    <input id="date_of_birth" class="{{ $errors->has('date_of_birth') ? ' invalid' : '' }} datepicker" type="text" value="{{ old('date_of_birth') }}" name="date_of_birth"
                           required>
                    @if ($errors->has('date_of_birth'))
                        <script>M.toast({html: '{{ $errors->first('date_of_birth') }}'})</script>
                    @endif
                </div>
                <div class="input-field col s6">
                    <select id="function" name="function">
                        <option value="" disabled selected>Choose a Function</option>
                        <option value="teamleader">Team Leader</option>
                        <option value="trafficdesk">Trafficdesk</option>
                        <option value="foreman">Foreman</option>
                        <option value="warehouse_operator">Warehouse Operator</option>
                    </select>
                    @if ($errors->has('function'))
                        <script>M.toast({html: '{{ $errors->first('function') }}'})</script>
                    @endif
                    <label for="function">Function</label>
                </div>
            </div>
            <div class="row">
                <div class="input-field col s1">
                    <p class="left-align">Reachtruck Certificate?</p>
                </div>
                <div class="input-field col s1 offset-s1">
                    <label>
                        <input class="with-gap" name="reachtruck_cert" type="radio" value="1"/>
                        <span>Yes</span>
                    </label>
                </div>
                <div class="input-field col s3">
                    <label>
                        <input class="with-gap" name="reachtruck_cert" type="radio" value="0"/>
                        <span>No</span>
                    </label>
                    @if ($errors->has('reachtruck_cert'))
                        <script>M.toast({html: '{{ $errors->first('reachtruck_cert') }}'})</script>
                    @endif
                </div>
                <div class="input-field col s6">
                    <label for="reachtruck_cert_expiry_date">Reachtruck Certificate Expiry Date</label>
                    <input id="reachtruck_cert_expiry_date" class="{{ $errors->has('reachtruck_cert_expiry_date') ? ' invalid' : '' }} datepicker" type="text"
                           value="{{ old('reachtruck_cert_expiry_date') }}" name="reachtruck_cert_expiry_date">
                    @if ($errors->has('reachtruck_cert_expiry_date'))
                        <script>M.toast({html: '{{ $errors->first('reachtruck_cert_expiry_date') }}'})</script>
                    @endif
                </div>
            </div>
            <div class="row">
                <div class="input-field col s1">
                    <p class="left-align">forklift Certificate?</p>
                </div>
                <div class="input-field col s1 offset-s1">
                    <label>
                        <input class="with-gap" name="forklift_cert" type="radio" value="1"/>
                        <span>Yes</span>
                    </label>
                </div>
                <div class="input-field col s3">
                    <label>
                        <input class="with-gap" name="forklift_cert" type="radio" value="0"/>
                        <span>No</span>
                    </label>
                    @if ($errors->has('forklift_cert'))
                        <script>M.toast({html: '{{ $errors->first('forklift_cert') }}'})</script>
                    @endif
                </div>
                <div class="input-field col s6">
                    <label for="forklift_cert_expiry_date">Forklift Certificate Expiry Date</label>
                    <input id="forklift_cert_expiry_date" class="{{ $errors->has('forklift_cert_expiry_date') ? ' invalid' : '' }} datepicker" type="text"
                           value="{{ old('forklift_cert_expiry_date') }}" name="forklift_cert_expiry_date">
                    @if ($errors->has('forklift_cert_expiry_date'))
                        <script>M.toast({html: '{{ $errors->first('forklift_cert_expiry_date') }}'})</script>
                    @endif
                </div>
            </div>
            <div class="card-action right-align">
                <button class="waves-effect waves-light btn-small hoverable" type="submit">Add Employee</button>
            </div>
        </form>
    </div>
</div>

RegisterController.php:

namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{


use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/users';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'name'             => 'required|string|max:255',
        'lastname'         => 'required|string|max:255',
        'initials'         => 'required|string|max:10',
        'email'            => 'required|string|email|max:255|unique:users',
        'password'         => 'required|string|min:6|confirmed',
        'mobile_number'    => 'required|string|max:25',
        'emergency_number' => 'required|string|max:25',
        'date_of_birth'    => 'required|date',
        'contract_type'    => 'required|string|max:50',
        'function'         => 'required|string|max:50',
        'reachtruck_cert'  => 'required|integer',
        'forklift_cert'    => 'required|integer',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'name'                        => $data['name'],
        'lastname'                    => $data['lastname'],
        'initials'                    => $data['initials'],
        'email'                       => $data['email'],
        'password'                    => Hash::make($data['password']),
        'mobile_number'               => $data['mobile_number'],
        'emergency_number'            => $data['emergency_number'],
        'date_of_birth'               => $data['date_of_birth'],
        'contract_type'               => $data['contract_type'],
        'function'                    => $data['function'],
        'reachtruck_cert'             => $data['reachtruck_cert'],
        'reachtruck_cert_expiry_date' => $data['reachtruck_cert_expiry_date'],
        'forklift_cert'               => $data['forklift_cert'],
        'forklift_cert_expiry_date'   => $data['forklift_cert_expiry_date'],
    ]);
}
}

User model:

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    use Notifiable;
    
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'lastname',
        'initials',
        'email',
        'password',
        'mobile_number',
        'emergency_number',
        'date_of_birth',
        'contract_type',
        'function',
        'reachtruck_cert',
        'reachtruck_cert_expiry_date',
        'forklift_cert',
        'forklift_cert_expiry_date',
    ];
    
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];
    
}

php artisan route:list

+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method    | URI                    | Name             | Action                                                                 | Middleware   |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD  | /                      | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | GET|HEAD  | api/user               |                  | Closure                                                                | api,auth:api |
|        | GET|HEAD  | home                   | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | POST      | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | GET|HEAD  | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST      | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | POST      | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | POST      | password/reset         |                  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD  | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | GET|HEAD  | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,auth     |
|        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,auth     |
|        | GET|HEAD  | users                  | users.index      | App\Http\Controllers\UserController@index                              | web          |
|        | POST      | users                  | users.store      | App\Http\Controllers\UserController@store                              | web          |
|        | GET|HEAD  | users/create           | users.create     | App\Http\Controllers\UserController@create                             | web          |
|        | GET|HEAD  | users/{user}           | users.show       | App\Http\Controllers\UserController@show                               | web          |
|        | PUT|PATCH | users/{user}           | users.update     | App\Http\Controllers\UserController@update                             | web          |
|        | DELETE    | users/{user}           | users.destroy    | App\Http\Controllers\UserController@destroy                            | web          |
|        | GET|HEAD  | users/{user}/edit      | users.edit       | App\Http\Controllers\UserController@edit                               | web          |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+

And last (even though I don't think it has something to do with my problem because my register.blade.php sends it directly to the registercontroller.) I will show my UserController;

namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
/**
 * Class UserController
 * @package App\Http\Controllers
 */
class UserController extends Controller
{
    
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $users = User::latest()->paginate(5);
        
        
        return view('users.index', compact('users'))->with('i', (request()->input('page', 1) - 1) * 5);
    }
    
    
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('auth.register');
    }
    
    
    /**
     * @param Request $request
     */
    public function store(Request $request)
    {
        // Not using this here.
    }
    
    
    /**
     * @param User $user
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(User $user)
    {
        return view('users.show', compact('user'));
    }
    
    
    /**
     * @param User $user
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(User $user)
    {
        return view('users.edit', compact('user'));
    }
    
    
    /**
     * @param Request $request
     * @param User $user
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, User $user)
    {
        request()->validate([
            'name'             => 'required|string|max:255',
            'lastname'         => 'required|string|max:255',
            'initials'         => 'required|string|max:10',
            'email'            => 'required|string|email|max:255',
            'mobile_number'    => 'required|string|max:25',
            'emergency_number' => 'required|string|max:25',
            'date_of_birth'    => 'required|date',
            'contract_type'    => 'required|string|max:50',
            'function'         => 'required|string|max:50',
            'reachtruck_cert'  => 'required|integer',
            'forklift_cert'    => 'required|integer',
        ]);
        
        
        $user->update($request->all());
        return redirect()->route('users.index')->with('success', 'Employee updated successfully');
    }
    
    
    /**
     * @param User $user
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function destroy(User $user)
    {
        $user->delete();
        return redirect()->route('users.index')->with('success', 'Employee deleted successfully');
    }
}

Is there anyone that could assist me with my problem? I'm stuck on this for some hours now. If you need any more files let me know and I will update this post.

0 likes
9 replies
36864's avatar

I'm fairly sure fields are case-sensitive. You're using "Forklift_cert" in your frontend and "forklift_cert" in your backend.

woxene's avatar

I am sorry, I know! I fixed that already. I still have the same problem tho!

woxene's avatar

I updated it!

Also, I ran return response()->json($request) in the registercontroller and I got this result:

"_token": "5GwdLhezd2EeKQYQYcn0cYT47NxlK6zzVGPwqTsj",
"name": "testname",
"lastname": "testlast",
"initials": "tt",
"email": "[email protected]",
"password": "testpassword",
"password_confirmation": "testpasssword",
"mobile_number": "0612345678",
"emergency_number": "0612345678",
"date_of_birth": "22-8-2018",
"function": "teamleader",
"reachtruck_cert": "0",
"reachtruck_cert_expiry_date": null,
"forklift_cert": "0",
"forklift_cert_expiry_date": null
}```
36864's avatar

Dump out the data array in the create method to check if you're even hitting it.

protected function create(array $data)
{
    dump($data);
    //rest of your code
AddWebContribution's avatar

Your Mistake is the same route name in web.php

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

So, First you change your same route name.

woxene's avatar

@36864 Thank you for your answer. I tried it and my page refreshes without any error message. Nothing was saved to the database.. All value="{{ old('name') }}" date came back to the form.

woxene's avatar

@saurabhd I removed one of the routes (the one I didn't need) And the problem remains the same. Any idea?

36864's avatar

If you didn't get the data dump, your object probably didn't pass validation.

Dump out the errors variable in your view to make sure you're not missing anything:

@dump($errors)
woxene's avatar

Thank you!!!! That's it. I actually FORGOT to make a field for contract_type. Thats why there was an error. Thanks!

Please or to participate in this conversation.