身份验证错误或在尝试创建新用户时使用身份验证 laravel



我尝试在 laravel 5.7 中使用多个身份验证。它开始很好,我可以使用 laravel 制作的身份验证模型在用户中注册,错误代码是:

参数 1 传递给 Illuminate\Auth\SessionGuard::login(( 必须 实现接口 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  IlluminateContractsAuthAuthenticatable  $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 AppHttpControllersAuth;
use AppUser;
use AppHttpControllersController;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesValidator;
use IlluminateFoundationAuthRegistersUsers;
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 IlluminateContractsValidationValidator
     */
    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 AppUser
     */
    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 AppModels;
use RelieseDatabaseEloquentModel as Eloquent;
/**
 * Class User
 *
 * @property int $id
 * @property int $id_unit
 * @property string $name
 * @property string $email
 * @property CarbonCarbon $email_verified_at
 * @property string $password
 * @property string $remember_token
 * @property CarbonCarbon $created_at
 * @property CarbonCarbon $updated_at
 *
 * @property AppModelsUnit $unit
 *
 * @package AppModels
 */
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(AppModelsUnit::class, 'id_unit');
    }
}

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

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

use IlluminateFoundationAuthUser as Authenticatable;

并实现具有可身份验证的类用户


use RelieseDatabaseEloquentModel as Eloquent;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Eloquent implements Authenticable;
{
--your implementation--
}

最新更新