[英]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.