简体   繁体   中英

Laravel: PHP Artisan Tinker 'SQLSTATE[23000]' error

So I am going through a series of intro videos to laravel and I am at the Database migration part and am struggling with a few things...

Here is the error... 在此处输入图片说明

I have a) no idea what it means or is referring to as this is my first foray into real command prompt usage and b) how to fix it.

Any help would be much appreciated.

regards.

EDIT-1: Here is my migration file...

<?php

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

class CreateCardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cards', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('cards');
    }
}

EDIT 2: Here is my command prompt, it is working with the answer below, but now with the original laracast tutorial. 在此处输入图片说明

It means that you have to supply a user-id as your schema dictates that a card belongs to a user. Add ['user_id'=>some user id] to your array.

Alternatively, create a cards relation on your User -model:

class User extends \Illuminate\Database\Eloquent\Model
{
    public function cards()
   {
      return $this->hasMany(Card::class);
   }
}

This way, you can create a card like so (assuming you have a user variable, fx. by calling User::first() , User::find($id) etc.):

$user->cards()->create([...])

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