3
votes

I cretead model, with columns:

like4u_id, vtope_id, panel_id

I set these columns option - Nullable, but if i save model, i get error:

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'like4u_id' at row 1 (SQL: update master_vars set updated_at = 2018-05-23 16:03:14, like4u_id = , vtope_id = where id = 328)

What is the problem? It's field not required and column nullable..

2
Have you updated a database table?u_mulder
Have you added all fields in $fillable?Chirag Patel
do you save it manually or using backend forms.. if saving manually can you please share your code.. how you save it.Hardik Satasiya

2 Answers

1
votes

It may happen with MySQL databases. You need to use the Nullable trait in your model for set to NULL the attributes when left empty.

Here is the docs of the Nullable trait

1
votes

When a form field is empty but should contain an "integer" value in the database some engines will try to insert an empty string into an integer field.
This is what causes the error message, basically trying to put the square through the round hole.

If you implement the Nullable trait you can make sure that these fields, if left empty are inserted as a true NULL instead of a string

I prefer to have my trait as a use statement above the class and then a short use int the class itself.
You can also put it directly in the class by use \October\Rain\Database\Traits\Nullable; instead of use Nullable; if you don't wish to have it appear in two spots. It's up to your preference.

use Model;
use October\Rain\Database\Traits\Nullable; 
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/**
 * Page Model
 */
class ModuleLink extends Model
{
    public $table = 'your_table_name_here';

    use Nullable; // This sets the trait to be used in this Model.
  //^^^^^^^^^^^^^
    public $nullable = [
        'module_id',    // Define which fields should be inserted as NULL when empty
        'sort_order',
    ];