gpt4 book ai didi

新用户注册时 Laravel 5.8 无法生成 api_token

转载 作者:行者123 更新时间:2023-12-04 14:22:11 25 4
gpt4 key购买 nike

我正在玩 Laravel 身份验证。

在使用 Composer 新创建的 Laravel 应用程序上,我按照字面上的说明进行操作直到这一点(包括在内)

https://laravel.com/docs/5.8/api-authentication#generating-tokens

但是,当我注册新用户时,api_token 字段为 NULL。

我还需要做什么才能在用户注册时开始生成 api token ?

在 RegisterController 中创建方法:

protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(60),
]);
}

迁移(我称之为 token )更新用户表:
class Token extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('api_token', 80)->after('password')
->unique()
->nullable()
->default(null);
});
}

应用\用户模型:
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use 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',
];

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

最佳答案

在您的用户模型中,将 'api_token' 添加到您的可填写内容中

class User extends Authenticatable
{
use Notifiable;

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

关于新用户注册时 Laravel 5.8 无法生成 api_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55599742/

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