dongyi1982 2017-11-13 19:24 采纳率: 0%
浏览 79
已采纳

我一直收到这个错误:字段'api_token'在PHP Laravel 5.5中没有默认值

I'm new to laravel. Im trying to make an 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. 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))

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

}

  • 写回答

3条回答 默认 最新

  • dopod0901 2017-11-14 00:10
    关注

    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',

    <?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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?