首页 > 解决方案 > Laravel 5.8 无法获取 api token 字段填写

问题描述

我正在尝试使用 Laravel 5.8 完成一个 api 注册系统。每次我运行注册时,api_token 字段都保持为空。我很确定我正确地遵循了说明,但我仍然遇到错误。

这是我创建的迁移。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UpdateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //

        Schema::table('users', function ($table) {
    $table->string('api_token', 80)->after('password')
                        ->unique()
                        ->nullable()
                        ->default(null);
});

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

以下是注册控制器中的创建方法。

/**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'api_token' => Str::random(60),
        ]);
    }

这几乎就像在此过程中完全忽略了“api_token”字段。所有其他字段均在此过程中完成。

标签: laravel

解决方案


首先让我看看:您是否在用户模型的用户$ fillable 数组中添加了“api_token”


推荐阅读