jeudi 25 avril 2019

Storing a new database entry using Controller Store/Create operations returns SQLSTATE[HY000]: General error: 1364

When trying to store an entry in my Database using Eloquent ORM i am getting the error,

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

However in my migrations i never specify name as being default as it is required.

My AnimalsController class.

  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 - Where the form data is entered

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

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');
    }
}

And in case it's worth anything my show.blade.php file.

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

I have played around for hours trying to get this to work and i am painfully blind to the solution.

I should probably also mention that if i use this code

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);
    }

I instead get this error.

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

Which i have also had no use fixing, i am using a resource in the web.php file

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

This led me to believe that $fillable was not working as intended and instead used the above method, however it still didn't work.

If i do make Name default in the Schema i will then have to give every other attribute a default value, which i don't want to do.

Hopefully i have given enough information to be of use, thank you :D



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire