简体   繁体   中英

I keep getting this error: Field 'api_token' doesn't have a default value in 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',
];

}

That error shows that the api_token field is empty when creating a new record. 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:

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:

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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