简体   繁体   English

问题:参数 #1 ($user) 必须是 Illuminate\Contracts\Auth\Authenticatable 类型,null 给定,调用

[英]Issue: Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, null given, called in

I'm beginner in Laravel.我是 Laravel 的初学者。 I'm using jetstream for auth.我正在使用 jetstream 进行身份验证。 When I trying register to database, it saves to database but gives this error.当我尝试注册到数据库时,它会保存到数据库但会出现此错误。 So how can I fix this problem那么我该如何解决这个问题

My HomeController is:我的 HomeController 是:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;

class HomeController extends Controller
{
    protected $user;


    public function __construct(){
        $this->middleware(function ($request, $next) {
            $this->user= Auth::user();

            return $next($request);
        });
    }

    public function main(){
        return view('welcome');
    }

    public function home(){
        return view('design2.index');
    }

    public function logout(Request $request) {
        Auth::logout();
        return redirect('/');
      }

    public function register(){
        return view('design2.registerpage');
    }

    public function login_page(){
        return view('design2.loginpage');
    }

    public function profile(){
        return view('profile');
    }

}

My user model is:我的用户 model 是:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];
}

My CreateNewUser is:我的 CreateNewUser 是:

<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    /**
     * Validate and create a newly registered user.
     *
     * @param  array  $input
     * @return \App\Models\User
     */
    public function create(array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(),
            
            
        ])->validate();

        $save = User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
        ]);
 

    }
}

Error is:错误是:

Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, null given, called in /Applications/XAMPP/xamppfiles/htdocs/muzik/vendor/laravel/fortify/src/Http/Controllers/RegisteredUserController.php on line 56 Illuminate\Auth\SessionGuard::login(): 参数 #1 ($user) 必须是 Illuminate\Contracts\Auth\Authenticatable 类型,null 给定,在 /Applications/XAMPP/xamppfiles/htdocs/muzik/vendor/laravel/ 中调用fortify/src/Http/Controllers/RegisteredUserController.php 在第 56 行

Try to return created user instead of just save it in variable $save .尝试返回创建的用户,而不是将其保存在变量$save中。

    public function create(array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(),                     
        ])->validate();

        return User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
        ]);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 社交名流:传递给 Illuminate\Auth\SessionGuard::login() 的参数 1 必须实现接口 Illuminate\Contracts\Auth\Authenticatable,给定 null - Socialite : Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given Laravel Messenger-传递给Illuminate \\ Auth \\ Guard :: login()的参数1必须实现Illuminate \\ Contracts \\ Auth \\ Authenticatable接口,给定null - Laravel messenger - Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given TypeError: Illuminate\Auth\SessionGuard::login(): 参数 #1 ($user) 必须是类型 Illuminate\Contracts\Auth\Authenticatable,, App\Models\Admin giv - TypeError: Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable,, App\Models\Admin giv LARAVEL 7.0 传递给 Illuminate\\Auth\\SessionGuard::login() 的参数 1 必须实现接口 Illuminate\\Contracts\\Auth\\Authenticatable,给定数组 - LARAVEL 7.0 Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, array given 传递给 Illuminate\Auth\EloquentUserProvider::validateCredentials() 的参数 1 必须是 Illuminate\Contracts\Auth\Authenticatable 的实例 - Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable Laravel 8:传递给 Illuminate\Auth\SessionGuard::__construct() 的参数 2 必须是 Illuminate\Contracts\Auth\UserProvider 的实例,null 给定 - Laravel 8 : Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given 传递给 Arubaao\\BasicAuth\\BasicGuard::__construct() 的参数 2 必须是 Illuminate\\Contracts\\Auth\\UserProvider 的实例,给定 null - Argument 2 passed to Arubacao\BasicAuth\BasicGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given 不能使用 Illuminate\Contracts\Auth\Authenticatable - 这不是一个特征 - Cannot use Illuminate\Contracts\Auth\Authenticatable - it is not a trait Illuminate\Auth\SessionGuard::login(): 参数 #1 ($user) 必须是类型 - Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type 类型错误:传递给 Illuminate\\Database\\Eloquent\\Builder::create() 的参数 1 必须是数组类型,给定的对象,在 laravel 中调用 - Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, object given, called in laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM