Laravel 5.6 方法 Illuminate\Database\Query\Builder::createToken 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50200173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:42:23  来源:igfitidea点击:

Laravel 5.6 Method Illuminate\Database\Query\Builder::createToken does not exist

laravellaravel-5

提问by condo1234

I am trying to create API authentication using Passport but I can not seem to create a token when a user is being registered using createToken().

我正在尝试使用 Passport 创建 API 身份验证,但是当使用createToken()注册用户时,我似乎无法创建令牌。

I have already checked I have included HasApiTokens but still gives same error.

我已经检查过我已经包含了 HasApiTokens 但仍然给出相同的错误。

ERROR

错误

Method Illuminate\Database\Query\Builder::createToken does not exist

方法 Illuminate\Database\Query\Builder::createToken 不存在

App\User

应用\用户

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * 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',
    ];
}

SignupController

注册控制器

**public function userLogin(Request $request) 
{
    $email = $request->email;
    $password = $request->password;

    $user = User::where('email' , $email)->where( 'password' , $password);
    if($user)
    {
        $token = $user->createToken('MyApp')->accessToken;
        $arr = array('token' => $token, 'status' => 'isTrue', 'userId' => $data[0]->id);
        //return response()->json($arr , 200);
    }**
}

回答by Paul

You need to fetch the user. Currently $user is the QueryBuilder, not the user object.

您需要获取用户。目前 $user 是 QueryBuilder,而不是用户对象。

User::where('email', $email)->where('password', $password)->first();

User::where('email', $email)->where('password', $password)->first();

回答by Muhammad Nauman

You need to add the methods like get()or first()or 'firstOrFail()' to get the database result. all the wherechains just return the QueryBuilder's object. Second thing is that you won't be saving the password as plain text (if you are saving so then please change it and hashed it before saving). For your case, it would become:

您需要添加诸如get()orfirst()或 'firstOrFail()' 之类的方法来获取数据库结果。所有的where链都只返回QueryBuilder的对象。第二件事是您不会将密码保存为纯文本(如果您正在保存,那么请在保存之前更改它并对其进行哈希处理)。对于您的情况,它将变为:

$user = User::where('email' , $email)->where( 'password' , $password)->first();

In the scenario of hashed password:

在散列密码的情况下:

$user = User::where('email' , $email)->first();
if(Hash::check(optional($user)->password, $request->password)) {
    // your code here
}

回答by Sagar Roy

You need to add the

您需要添加

use Laravel\Passport\HasApiTokens;

class User extends Authenticatable {

use HasApiTokens,
    Notifiable;

trait to your User model.

您的 User 模型的特征。