skip to Main Content

I have a column enabled with datatype bit(1). I am trying to save 0 or 1 value in Database by Laravel eloquent.

$model->enabled = $inputs['enabled'];
$model->save();

I have saved values in my config file.

'enabled' => [
    '0' => 'No',
    '1' => 'Yes',
],

But when I tried to save these value in database. I was getting error like,

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column ‘enabled’ at row 1

when i ran query in mysql phpmyadmin, it saved data correctly database.

But running this query by eloquent, it produces error.

5

Answers


  1. Chosen as BEST ANSWER

    So i had solved my question by just adding DB::raw();

    like,

    $model->enabled = DB::raw($inputs['enabled']);
    $model->save();
    

    Eloquent doesn't work by calling Boolean value directly, first you have to gone through your values in DB::raw();

    It works like a charm.


  2. you need tinyint type, in your migration file do

    $table->boolean('enabled')->default(false);
    

    When you migrate with boolean('column_name') it creates a column with type tinyint(1) and you can set to model true/1 or false/0 and save. For example

    $model->boolean_field = true;
    $model->save();
    

    Or

    $model->boolean_field = 1;
    $model->save();
    
    Login or Signup to reply.
  3. As per MySQL manual you can use should use bool and boolean which are aliases of tinyint to store 0 or 1 value

    TINYINT: A value of
    zero is considered false. Non-zero
    values are considered true.

    So use:

    $table->tinyInteger('enabled');
    

    https://dev.mysql.com/doc/refman/8.0/en/other-vendor-data-types.html

    Login or Signup to reply.
  4. A boolean in mysql is generaly a tinyint.

    Eloquent models can cast boolean automaticaly : https://laravel.com/docs/6.x/eloquent-mutators#attribute-casting

    protected $casts = [
        'enabled' => 'boolean',
    ];
    

    Before saving your model, make sure your variable is a boolean

    $model->enabled = $inputs['enabled']==='Yes';
    $model->save();
    
    Login or Signup to reply.
  5. also you can use mutator to force values.
    example : in your model, append :

     public function setEnabled($value) // note the E in uppercase.
        {
            if ($value=='YES') { $this->attributes['enabled'] = 1;
            if ($value=='NO')  { $this->attributes['enabled'] = 0;
            if ($value=='1')  { $this->attributes['enabled'] = 1;
            if ($value=='0')  { $this->attributes['enabled'] = 0;
            // etc...
            // the logic can be improved... yes/YES/No/no/0/1/empty/null/ etc..
        }
    

    and in the client code

       $model->enabled = 'YES'; // || 'NO' || etc..,  : note the e in lowercase.
    

    then method setEnabled is called during assignation and ‘enabled’ will become 1

    doc : https://laravel.com/docs/5.8/eloquent-mutators

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search