简体   繁体   English

我不断收到此错误:PHP Laravel 5.5中的字段“ api_token”没有默认值

[英]I keep getting this error: Field 'api_token' doesn't have a default value in PHP Laravel 5.5

I'm new to laravel. 我是laravel的新手。 Im trying to make an api. 我正在尝试制作一个api。 So if you register as a (auth)user, u'd be given an api_token for you to have an access to the dashboard Here's some of my codes. 因此,如果您注册为(auth)用户,将获得一个api_token以便您可以访问仪表板。这是我的一些代码。 And when I try to register it gives me this error: 当我尝试注册时,出现此错误:

Illuminate \\ Database \\ QueryException (HY000) SQLSTATE[HY000]: General error: 1364 Field 'api_token' doesn't have a default value (SQL: insert into users ( name , email , password , updated_at , created_at ) values (goddies, f@gmail.com, $2y$10$uKJPI9hBJSdygMf7MefP1eM1GQ7VM3s74eVy5qcuFj4/s8HH2Iun., 2017-11-13 19:11:38, 2017-11-13 19:11:38)) Illuminate \\ Database \\ QueryException(HY000)SQLSTATE [HY000]:常规错误:1364字段“ api_token”没有默认值(SQL:插入usersnameemailpasswordupdated_atcreated_at ))值(goddies,f @ gmail.com,$ 2y $ 10 $ uKJPI9hBJSdygMf7MefP1eM1GQ7VM3s74eVy5qcuFj4 / s8HH2Iun。,2017-11-13 19:11:38,2017-11-13 19:11:38)

On my migration for the user: 在为用户迁移时:

<?php

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

class CreateUsersTable extends Migration
{

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('api_token', 20)->unique();
        $table->rememberToken();
        $table->timestamps();
    });
}


public function down()
{
    Schema::dropIfExists('users');
}

} }

here's my user model: 这是我的用户模型:

<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;

protected $fillable = [
    'name', 'email', 'password', 'api_token',
];

protected $hidden = [
    'password', 'remember_token', 'api_token',
];

} }

That error shows that the api_token field is empty when creating a new record. 该错误表明在创建新记录时api_token字段为空。 That's why the error shows that "it doesn't have a default value". 这就是为什么错误会显示“它没有默认值”的原因。

Modify your store method in your UserController (or the one that you use to save a new record) and generate a value for the api_token field: UserController修改store方法(或用于保存新记录的store方法),并为api_token字段生成一个值:

public function store(Request $request)
{
    // some validation logic
    $user = new User;
    $user->name = $request->name;
    $user->email = $request->email;
    $user->password = $request->password;
    // ... the rest of your fields

    // to generate a new token for the new user
    $user->api_token = str_random(20);

    // then save
    $user->save();

    return $user;
}

Obs: Obs:

If your using a factory/seeder, you will need to generate that field or make nullable that column in the database. 如果您使用工厂/播种机,则需要生成该字段或使数据库中的该列可为空。

That error shows that the api_token field is empty or not set as a NULL at the time of creating a new record. 该错误表明在创建新记录时api_token字段为空或未设置为NULL That's why the error shows that it doesn't have a default value 这就是为什么该错误表明它没有默认值的原因

so you can simply modify column from users table and set default value as a NULL and insert new record. 因此您可以简单地修改用户表中的列,并将default值设置为NULL并插入新记录。

If you want to prevent this error, you need to set your column 'api_token' to nullable so it has a default value 'NULL' if there is no value coming from your request of 'api_token', 如果您想避免发生此错误,则需要将列“ api_token”设置为可空值,以便在您的请求“ api_token”没有值的情况下将其默认值设置为“ NULL”,

<?php

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

class CreateUsersTable extends Migration
{

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('api_token', 20)->unique()->nulllable();
        $table->rememberToken();
        $table->timestamps();
    });
}


public function down()
{
    Schema::dropIfExists('users');
}

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM