Php 错误:一般错误:Laravel中的1364字段

Php 错误:一般错误:Laravel中的1364字段,php,laravel,Php,Laravel,我得到以下错误 SQLSTATE[HY000]:一般错误:1364字段“AccountNumber”不存在 具有默认值(SQL:insert intoaccounts(AccountClass), 在处更新,在处创建)值(2019-10-23 08:58:292019-10-23 08:58:29) 控制器: namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Accounts; class AccountC

我得到以下错误

SQLSTATE[HY000]:一般错误:1364字段“AccountNumber”不存在 具有默认值(SQL:insert into
accounts
AccountClass
),
处更新,
处创建)值(2019-10-23 08:58:292019-10-23 08:58:29)

控制器:
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Accounts;
class AccountController extends Controller
{
    public function show(){
        return view ('upload');
    }
    public function store(Request $request){


        $file = $request->file('upload-file');
        $csvData = file_get_contents($file);

        $rows = array_map("str_getcsv", explode("\n", $csvData));

        $header = array_shift($rows);
//    dd($header);
        foreach ($rows as $row) {
            $row = array_combine($header, $row);
            Accounts::create([
                'AccountClass' => $row['Classe'],
                'AccountNumber' => $row['Compte'],
                'AccountDesc' => $row['Desc'],
                'active' => 1,
            ]);
        }


    }

}
迁移:

<?php

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

class CreateAccountsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('accounts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('AccountClass');
            $table->integer('AccountNumber');
            $table->string('AccountDesc');
            $table->timestamps();
        });
    }

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

在迁移过程中,可以这样做以使列可为空:

public function up()
{
        Schema::create('accounts', function (Blueprint $table) {
                $table->integer('AccountNumber')->nullable();
        }); 
}

->nullable()

public function up()
{
        Schema::create('accounts', function (Blueprint $table) {
                $table->integer('AccountNumber')->nullable();
        }); 
}
->nullable()
指定列允许空值

  • 回滚CreateAccountsTable迁移文件
  • 编辑此迁移文件并替换
  • 迁移迁移文件并再次测试它
  • 回滚CreateAccountsTable迁移文件
  • 编辑此迁移文件并替换
  • 迁移迁移文件并再次测试它

  • 确保在您的帐户模型中它是可填充的

    Accounts.php模型

    class Accounts extends Model
    {    
         protected $fillable = [
            'AccountClass', 'AccountNumber', 'AccountDesc',
        ];  
    }
    

    如果在插入时未传递值,请将默认值设置为
    NULL

    确保在帐户模型中它是可填充的

    Accounts.php模型

    class Accounts extends Model
    {    
         protected $fillable = [
            'AccountClass', 'AccountNumber', 'AccountDesc',
        ];  
    }
    
    如果插入时未传递值,则将默认值设置为
    NULL