doushi6932 2018-08-03 11:14
浏览 85
已采纳

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

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');
    }
}
  • 写回答

1条回答 默认 最新

  • doufan1899 2018-08-03 14:45
    关注

    I found out what I did wrong. I never made the input for contract_type in my blade template. It didn't get through my validation because of this reason. Found out by putting @dump($errors) in the top op my blade template.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!