尝试创建新用户时使用身份验证Laravel的身份验证错误

时间:2019-06-13 19:54:52

标签: php laravel authentication laravel-5 laravel-authentication

我试图在laravel 5.7中使用多种身份验证。一切正常,我可以使用laravel制作的身份验证模型在用户中注册,错误代码为:

  

传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须   实现接口Illuminate \ Contracts \ Auth \ Authenticatable,为null   给定,称为
  C:\ xampp-7.3.5 \ htdocs \ jatimsakti \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Auth \ RegistersUsers.php   在第35行◀


    /**
     * Log a user into the application.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param  bool  $remember
     * @return void
     */
    public function login(AuthenticatableContract $user, $remember = false)
    {
        $this->updateSession($user->getAuthIdentifier());

        // If the user should be permanently "remembered" by the application we will
        // queue a permanent cookie that contains the encrypted copy of the user
        // identifier. We will then decrypt this later to retrieve the users.
        if ($remember) {
            $this->ensureRememberTokenIsSet($user);

            $this->queueRecallerCookie($user);
        }

        // If we have an event dispatcher instance set we will fire an event so that
        // any listeners will hook into the aut

我试图设置登录控制器,但是我从未接触过laravel的任何身份验证模型

我的注册刀片是

@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">{{ __('Register') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('register') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

                                @error('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">{{ __('E-Mail Address') }}</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="unit" class="col-md-4 col-form-label text-md-right">{{ __('Unit') }}</label>

                            <div class="col-md-6">
                              <select id="unit" name="unit" class="form-control">
                                <option value="1">OJK</option>
                                <option value="2">Humas</option>
                            </select>

                          </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</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">{{ __('Confirm Password') }}</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">
                                    {{ __('Register') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

这是我的路线

Route::get('/', function () {
    return view('home');
});

Auth::routes();

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

//Fitur Pengajuan Anggaran
Route::get('daftar', 'PegawaiController@showDaftarAnggaran')->name('daftar.anggaran');

Route::get('form', 'PegawaiController@showFormAnggaran')->name('form.anggaran');
Route::POST('form', 'PegawaiController@createFormAnggaran')->name('post.form.anggaran');
Route::post('/form/upload', 'PegawaiController@proses_upload');

Route::get('unit','PegawaiController@showAnggaranUnit')->name('unit.anggaran');
Route::get('daftar/{id}', 'PegawaiController@showDaftarAnggaranUnit')->name('daftar.anggaran.unit');

这里是我的控制器

<?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
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

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

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

    /**
     * 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'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
      User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'id_unit'  => $data['unit'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

这是我的用户模型

<?php

/**
 * Created by Reliese Model.
 * Date: Sat, 01 Jun 2019 15:27:42 +0000.
 */

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;

/**
 * Class User
 *
 * @property int $id
 * @property int $id_unit
 * @property string $name
 * @property string $email
 * @property \Carbon\Carbon $email_verified_at
 * @property string $password
 * @property string $remember_token
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 *
 * @property \App\Models\Unit $unit
 *
 * @package App\Models
 */
class User extends Eloquent
{
    protected $casts = [
        'id_unit' => 'int'
    ];

    protected $dates = [
        'email_verified_at'
    ];

    protected $hidden = [
        'password',
        'remember_token'
    ];

    protected $fillable = [
        'name',
        'email',
        'id_unit',
        'email_verified_at',
        'password',
        'remember_token'
    ];

    public function unit()
    {
        return $this->belongsTo(\App\Models\Unit::class, 'id_unit');
    }
}

我也希望将id单位也添加到单位表中,这是我唯一的自定义字段,数据是通过php我的管理员输入的,但是当我尝试创建时发生了错误

1 个答案:

答案 0 :(得分:0)

为什么不尝试将这行代码添加到用户模型中

use Illuminate\Foundation\Auth\User as Authenticatable;

并实现具有Authenticable的User类


use Reliese\Database\Eloquent\Model as Eloquent;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Eloquent implements Authenticable;
{
--your implementation--
}