3
votes

When I try to save or update a model, I get an error that is below, for the field spid_id. I am not sure what is wrong.

General error: 1366 Incorrect integer value: '' for column 'spid_id' at row 1 (SQL: update magazines set spid_id = , updated_at = 2016-10-21 08:28:46, summary = where id = 8)

This is how my table looks:

       Schema::create('magazines', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('visual_id')->nullable();
            $table->integer('spid_id')->nullable();
            $table->timestamps();
        }); 

I tried to change my spid_id to not be nullable in the DB, by making a migration, because I thought that might be a reason:

        Schema::table('magazines', function (Blueprint $table) {
            $table->integer('spid_id')->change();
        });

But the field still remained nullable.

This is my store function for create form:

        $magazine = Magazine::create([
          'name' => $request->input('name'),
          'visio_link_prefix' => $request->input('visio_link_prefix'),
          'spid_id' => $request->input('spid_id'),
          'summary' => $request->input('summary'),
        ]);
2
Show us the place where you updateing in LaravelFilip Koblański
If the value is blank then dont set the key. It will try to save the data as integer (without 's). Or add quotes around the value or filter the array.Sougata Bose
Could you please post code where the error happens. So code updating or saving model?boroboris
Can you try 'spid_id' => $request->has('spid_id') ? $request->input('spid_id') : NULL,?aleksejjj
can you dd($request->input('spid_id')) ? What is the output?Goper Leo Zosa

2 Answers

5
votes

You need to check spid_id exist in your request before you save it. For example in your case it will looks like:

'spid_id' => $request->has('spid_id') ? $request->input('spid_id') : NULL,
1
votes

Another way to create a record is by using eloquent objects

    $magazine = new Magazine
    $magazine->name = $request->input('name');
    $magazine->visio_link_prefix = $request->input('visio_link_prefix')
    $magazine->spid_id = $request->input('spid_id', null);
    $magazine->summary = $request->input('summary');
    $magazine->save();

This will make sure it automatically assign a null or default value which you may have specified in your model.