Laravel 5.6寄存器不起作用,没有重定向,没有错误(已编辑的寄存​​器字段)

时间:2018-08-03 11:14:22

标签: php laravel controller

对于我的项目,我需要更改默认的php artisan make:auth User数据库+注册表格。我已经准备好一切,但似乎有些问题。

验证有效。因为我确实为此出错。但是,当我通过Validation函数时,似乎什么也没发生。我只是刷新一下,将所有字段都保留为value="{{ old('name') }}",没有任何内容添加到数据库中,也没有收到错误消息。

这是我的一些文件:

Web.php(路由):

<?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'],
    ]);
}
}

用户模型:

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          |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+

最后(尽管我不认为这与我的问题有关,因为我的register.blade.php将其直接发送到registercontroller。)我将显示我的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');
    }
}

1 个答案:

答案 0 :(得分:0)

我发现自己做错了。我从未在刀片模板中输入contract_type的内容。由于这个原因,它没有通过我的验证。通过将@dump($errors)放在我的刀片服务器模板的顶部来发现。