mardi 18 décembre 2018

SQLSTATE[HY000]: General error: 1364 Field 'starting_balance' doesn't have a default value

I am getting a very bizarre "Field doesn't have a default value" error. The field in question is starting_balance. It seems that Laravel is trying to save the starting_balance field into the database as if it was empty but I am sure it isn't. Any help appreciated.

I tried adding a default value for the field in the migration but that still doesn't help.

Here is my migration for the model:

<?php

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

class CreateApartmentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('apartments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('entrance_id');
            $table->integer('user_id')->nullable()->default(null);
            $table->integer('floor');
            $table->integer('apt_number');
            $table->string('square_meters', 64)->nullable();
            $table->decimal('percent_ideal_parts', 10, 3)->nullable();
            $table->decimal('starting_balance', 8, 2);
            $table->string('animals', 200)->nullable();
            $table->string('other_information', 2048)->nullable();
            $table->timestamps();
        });
    }

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

Here is the relevant part from the controller handling the request:

public function store(Request $request, ApartmentRequest $apartmentRequest)
{
    $apartment = new Apartment($request->except(['obekt_id', '_token']));

    if ($apartment->save())
    {
        Session::flash('success', 'Апартамента беше запазен успешно.');
        return redirect('/entrances/' . $apartment->entrance->id . '/apartments');
    }
    else
    {
        Session::flash('error', 'Имаше проблем докато запазвахме апартамента.');
        return back();
    }
}

Here is my model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Apartment extends Model
{
    protected $fillable = ['entrance_id', 'user_id', 'floor', 'apt_number', 'square_meters', 'percent_ideal_parts', 'starting_balance', 'animals', 'other_information'];

    public function people()
    {
        return $this->hasMany('App\Person');
    }

    public function entrance()
    {
        return $this->belongsTo('App\Entrance');
    }

    public function taksiDomoupravitel()
    {
        return $this->hasMany('App\TaksaDomoupravitel');
    }

    public function taksiFro()
    {
        return $this->hasMany('App\TaksaFro');
    }

    public function payments()
    {
        return $this->hasMany('App\Payment');
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire