使用Controller Store / Create操作存储新数据库条目将返回SQLSTATE [HY000]:常规错误:1364

问题描述 投票:0回答:1

当尝试使用Eloquent ORM在我的数据库中存储条目时,我收到错误,

Illuminate\Database\QueryException thrown with message "SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `animals` (`updated_at`, `created_at`) values (2019-04-25 22:35:47, 2019-04-25 22:35:47))"

但是在我的迁移中,我从未将name指定为默认值,因为它是必需的。

我的AnimalsController类。

  public function create()
    {
        return view('animals.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $animal = Animal::create([
          $request->input()
        ]);

        // $animal = new Animal;
        // $animal->name = $request->name;
        // $animal->dob = $request->dob;
        // $animal->description =  $request->description;
        // $animal->image = $request->image;
        // $animal->available = $request->available;
        //
        // echo $request->id;
        // echo "test";
        // echo $animal->id;

        // flash('Animal Added')->success();

        return redirect()->route('animals.show')->with('animal', $animal);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Animal  $animal
     * @return \Illuminate\Http\Response
     */
    public function show(Animal $animal)
    {
        return view('animals.show')->with('animal', $animal);
    }

Animal.php

class Animal extends Model
{

  use SoftDeletes;

  protected $dates = [
    'created_at',
    'updated_at'
  ];

  /**
   * Set the Attributes which are alllowed
   * to be assigned
   */
  protected $fillable = [
    'name',
    'dob',
    'description',
    'image',
    'available'
  ];

create.blade.php - 输入表单数据的位置

<div class="row">
  <div class="col">
    {!! Form::open(['route' => 'animals.store'], ['class' => 'form', 'files' => true]) !!}

    <div class="form-group">
      {!! Form::label('name', 'Animal Name', ['class' => 'control-label']) !!}
      {!! Form::text('name', null, ['class' => 'form-control input-lg', 'placeholder' => 'Waffles']) !!}
    </div>

    <div class="form-group">
      {!! Form::label('dob', "DOB", ['class' => 'control-label']) !!}
      {!! Form::date('dob') !!}
    </div>

    <div class="form-group">
      {!! Form::label('description', "Description", ['class' => 'control-label']) !!}
      {!! Form::textarea('description', null, ['size' => '20x3', 'class' => 'form-control input-lg', 'placeholder' => 'Describe the animal']) !!}
    </div>

    <div class="form-group">
      {!! Form::label('image', "Image", ['class' => 'control-label']) !!}
      {!! Form::file('image') !!}
    </div>

    <div class="form-group">
      {!! Form::label('available', "Available", ['class' => 'control-label']) !!}
      {!! Form::checkbox('available', 'value') !!}
    </div>

    <div class="form-group">
      {!! Form::submit('Add Animal', ['class' => 'submit btn btn-info btn-lg', 'style' => 'width: 100%']) !!}
    </div>

    {!! Form::close() !!}

  </div>

CreateAnimalsTable迁移

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

class CreateAnimalsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('animals', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 20);
            $table->date('dob');
            $table->mediumText('description');
            $table->string('image');
            $table->boolean('available')->default(false);
            $table->timestamps();
        });
    }

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

如果它值得我的show.blade.php文件。

<div class="container">
    <div class="row">
      <div class="col-xl-12 text-center">
          <p class="text-primary" style="margin-top:100px;">{{ $animal->name }} has animal ID #{{ $animal->id }}. Date: {{ $animal->dob }}</p>
      </div>
    </div>
</div>

我已经玩了几个小时试图让这个工作,我痛苦地对解决方案视而不见。

我可能还应该提一下,如果我使用这段代码

public function store(Request $request)
    {
        // $animal = Animal::create([
        //   $request->input()
        // ]);

        $animal = new Animal;
        $animal->name = $request->name;
        $animal->dob = $request->dob;
        $animal->description =  $request->description;
        $animal->image = $request->image;
        $animal->available = $request->available;

        echo $request->id;
        echo "test";
        echo $animal->id;

        // flash('Animal Added')->success();

        return redirect()->route('animals.show')->with('animal', $animal);
    }

我反而得到这个错误。

Illuminate\Routing\Exceptions\UrlGenerationException thrown with message "Missing required parameters for [Route: animals.show] [URI: animals/{animal}]."

我也没有使用修复,我在web.php文件中使用资源

Route::resource('animals', 'AnimalsController');

这让我相信$ fillable没有按预期工作,而是使用上面的方法,但它仍然无效。

如果我在Schema中使Name默认,那么我必须给每个其他属性一个默认值,我不想这样做。

希望我已经提供了足够的信息,谢谢你:D

php laravel-5 eloquent
1个回答
0
投票

我发现了两个问题。

首先,我不认为这会奏效

$animal = Animal::create([
    $request->input()
]);

这是一个包含在另一个数组中的数组。相反,让我们写:

$animal = Animal::create($request->input());

要么:

$animal = Animal::create($request->all());

在第二个示例中,我认为已创建动物,但未正确指定重定向路线:

return redirect()->route('animals.show')->with('animal', $animal);

让我们说:

return redirect()->route('animals.show', $animal->id);
© www.soinside.com 2019 - 2024. All rights reserved.