gpt4 book ai didi

php - Laravel 6:调用未定义的方法 App\\User::createToken()

转载 作者:行者123 更新时间:2023-12-05 08:31:26 26 4
gpt4 key购买 nike

我正在尝试生成 token 以通过以下方式在我的 Controller 中对用户进行身份验证:

namespace App\Http\Controllers\API;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\User;

class AuthController extends Controller
{
public function login()
{
if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
$user = Auth::user();


$success['token'] = $user->createToken('myApp')->accessToken;
dd($success['token']);

}
}

目前,我只是想打印出 token 。这是我的用户模型:

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
//use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, Notifiable;

const USER_FIRST_NAME_FIELD = "first_name";
const USER_LAST_NAME_FIELD = "last_name";
const USER_PREFERRED_NAME_FIELD = "preferred_name";
const USER_EMAIL_FIELD = "email";
const USER_EMAIL_VERIFIED_AT_FIELD = "email_verified_at";
const USER_PASSWORD_FIELD = "password";
const USER_REMEMBER_TOKEN_FIELD = "remember_token";
const USER_RECEIVE_NEWSLETTER_FIELD= "receive_newsletter";
const USER_ACTIVE_FIELD = "active";

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
self::USER_FIRST_NAME_FIELD,
self::USER_LAST_NAME_FIELD,
self::USER_PREFERRED_NAME_FIELD,
self::USER_EMAIL_FIELD,
self::USER_PASSWORD_FIELD,
self::USER_RECEIVE_NEWSLETTER_FIELD,
self::USER_ACTIVE_FIELD,
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
self::USER_PASSWORD_FIELD,
self::USER_REMEMBER_TOKEN_FIELD
];

/**
* Automatically creates password hash when password is submitted
*
* @param string $password
* @return void
*/
public function setPasswordAttribute(string $password) : void
{
$this->attributes['password'] = Hash::make($password);
}
}

如您所见,我正在使用 HasApiTokens, Notifiable 特征,但我从我的 Controller 中收到一条错误消息:

Call to undefined method App\User::createToken()

Passport 已正确安装和配置。

这里有一些奇怪的东西:

注册用户时(我正在使用单独的 Controller 并且还使用服务)成功创建 token :

这是我的 Controller :

<?php

namespace App\Http\Controllers\API;

use App\Services\UserService;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\RegisterUserRequest;


class UserController extends Controller
{
private $user;

public function __construct(UserService $user)
{
$this->user = $user;
}

public function store(RegisterUserRequest $request) : JsonResponse
{
// TODO: verify message on error

$user = $this->user->register($request->validated());
$token = $user->createToken('MyApp')->accessToken;
dd($token);

return response()->json(['status' => 201, 'user_id' => $user->id]);
}

}

这是我的服务:

<?php

namespace App\Services;

use App\Models\User;
use App\Services\BaseServiceInterface;

class UserService implements BaseServiceInterface
{

public function register(array $formValues) : User
{
// 'terms and conditions' should not be saved into the db, hence it's removed
unset($formValues['terms_conditions']);
return User::create($formValues);
}

}

这又是我的模型:

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
//use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, Notifiable;

const USER_FIRST_NAME_FIELD = "first_name";
const USER_LAST_NAME_FIELD = "last_name";
const USER_PREFERRED_NAME_FIELD = "preferred_name";
const USER_EMAIL_FIELD = "email";
const USER_EMAIL_VERIFIED_AT_FIELD = "email_verified_at";
const USER_PASSWORD_FIELD = "password";
const USER_REMEMBER_TOKEN_FIELD = "remember_token";
const USER_RECEIVE_NEWSLETTER_FIELD= "receive_newsletter";
const USER_ACTIVE_FIELD = "active";

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
self::USER_FIRST_NAME_FIELD,
self::USER_LAST_NAME_FIELD,
self::USER_PREFERRED_NAME_FIELD,
self::USER_EMAIL_FIELD,
self::USER_PASSWORD_FIELD,
self::USER_RECEIVE_NEWSLETTER_FIELD,
self::USER_ACTIVE_FIELD,
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
self::USER_PASSWORD_FIELD,
self::USER_REMEMBER_TOKEN_FIELD
];

正如我告诉您的,创建用户时会正确生成 token 。

我会说 Auth::user() 没有直接调用我的模型,但我不确定这是怎么回事。

知道为什么吗?谢谢

最佳答案

因为你的守卫返回了错误的User模型,App\User,你应该检查你的授权配置,'config/auth.php'。在 providers 数组中,将使用 App\User 模型的任何提供者(通常是 users)调整为 App\Models\User 代替。

'providers' => [
'users' => [
'driver' => 'eloquent',
// 'model' => App\User::class,
'model' => App\Models\User::class,
],
...
],

关于php - Laravel 6:调用未定义的方法 App\\User::createToken(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58443226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com