Archimedia Elettronica
Archimedia Elettronica

Reputation: 129

Why performInsert() removes field from array?

I don't understand why performInsert() deletes my '_logo' field in Laravel 5.2. My model has everything fillable except for 'id', 'created_at' and 'updated_at'.

(2/2) QueryException

SQLSTATE[HY000]: General error: 1364 Field '_logo' doesn't have a default value (SQL: insert into `LC_portatori` (`nome`, `cognome`, `tipo`, `pwd`, `telefono`, `indirizzo`, `email`, `updated_at`, `created_at`) values (Nome, CognomE, TipO, PwD, TelefonO, IndirizzO, emaiL, 2017-06-30 11:37:52, 2017-06-30 11:37:52))
in Connection.php (line 647)
at Connection->runQueryCallback('insert into `LC_portatori` (`nome`, `cognome`, `tipo`, `pwd`, `telefono`, `indirizzo`, `email`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)', array('Nome', 'CognomE', 'TipO', 'PwD', 'TelefonO', 'IndirizzO', 'emaiL', '2017-06-30 11:37:52', '2017-06-30 11:37:52'), object(Closure))
in Connection.php (line 607)
at Connection->run('insert into `LC_portatori` (`nome`, `cognome`, `tipo`, `pwd`, `telefono`, `indirizzo`, `email`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)', array('Nome', 'CognomE', 'TipO', 'PwD', 'TelefonO', 'IndirizzO', 'emaiL', '2017-06-30 11:37:52', '2017-06-30 11:37:52'), object(Closure))
in Connection.php (line 450)
at Connection->statement('insert into `LC_portatori` (`nome`, `cognome`, `tipo`, `pwd`, `telefono`, `indirizzo`, `email`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)', array('Nome', 'CognomE', 'TipO', 'PwD', 'TelefonO', 'IndirizzO', 'emaiL', '2017-06-30 11:37:52', '2017-06-30 11:37:52'))
in Connection.php (line 404)
at Connection->insert('insert into `LC_portatori` (`nome`, `cognome`, `tipo`, `pwd`, `telefono`, `indirizzo`, `email`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)', array('Nome', 'CognomE', 'TipO', 'PwD', 'TelefonO', 'IndirizzO', 'emaiL', '2017-06-30 11:37:52', '2017-06-30 11:37:52'))
in Processor.php (line 32)
at Processor->processInsertGetId(object(Builder), 'insert into `LC_portatori` (`nome`, `cognome`, `tipo`, `pwd`, `telefono`, `indirizzo`, `email`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)', array('Nome', 'CognomE', 'TipO', 'PwD', 'TelefonO', 'IndirizzO', 'emaiL', '2017-06-30 11:37:52', '2017-06-30 11:37:52'), 'id')
in Builder.php (line 2138)
at Builder->insertGetId(array('Nome', 'CognomE', 'TipO', 'PwD', 'TelefonO', 'IndirizzO', 'emaiL', '2017-06-30 11:37:52', '2017-06-30 11:37:52'), 'id')
in Builder.php (line 1247)
at Builder->__call('insertGetId', array(array('nome' => 'Nome', 'cognome' => 'CognomE', 'tipo' => 'TipO', 'pwd' => 'PwD', 'telefono' => 'TelefonO', 'indirizzo' => 'IndirizzO', 'email' => 'emaiL', 'updated_at' => '2017-06-30 11:37:52', 'created_at' => '2017-06-30 11:37:52'), 'id'))
in Model.php (line 684)
at Model->insertAndSetId(object(Builder), array('nome' => 'Nome', 'cognome' => 'CognomE', 'tipo' => 'TipO', 'pwd' => 'PwD', 'telefono' => 'TelefonO', 'indirizzo' => 'IndirizzO', 'email' => 'emaiL', 'updated_at' => '2017-06-30 11:37:52', 'created_at' => '2017-06-30 11:37:52'))
in Model.php (line 649)
at Model->performInsert(object(Builder))
in Model.php (line 518) 
at Model->save()
in Builder.php (line 734)
at Builder->Illuminate\Database\Eloquent\{closure}(object(Portatore))
in helpers.php (line 936)
at tap(object(Portatore), object(Closure))
in Builder.php (line 735)
at Builder->create(array('nome' => 'Nome', 'cognome' => 'CognomE', 'tipo' => 'TipO', 'pwd' => 'PwD', 'telefono' => 'TelefonO', 'indirizzo' => 'IndirizzO', 'email' => 'emaiL', '_logo' => 'nonceproprio'))
in Model.php (line 1357)

UPDATE

I process every field in the requests Form basically in this way:

public function store(Request $request){
    unset($request["_token"]);

    //get table name
    $tableName     = $request->segment(TABLE_SEGMENT);
    //convert to a class object
    $className     = 'App\\' . studly_case(str_singular($tableName));
    if(class_exists($className)) {
        $model = new $className;
        $model::create($request->all());
    }
}

And my model is:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Portatore extends Model
{
    //
    protected $table    = 'portatori';
    protected $guarded  = ['id','created_at','updated_at'];


}

My Form it's built starting from the model fields, so I give to the form every fields and I retrieve them back into a store function

Upvotes: 1

Views: 353

Answers (1)

Rwd
Rwd

Reputation: 35180

Looking at it the reason that you're unable to add _logo is because it starts with _ and it isn't in the fillable array. When the fillable array is empty Laravel will allow any column that isn't guarded and doesn't start with _.

You can either:

  • Remove the _ from the column name
  • Be explicit about with columns are fillable i.e.:

    protected $fillable = ['nome', 'cognome', 'tipo', 'pwd', 'telefono', 'indirizzo', 'email', '_logo']; //and any other columns that are allowed (if any)
    
  • Manually add the _logo value to the instance i.e. $model->_logo = $request['logo'];. (This will probably lead to "code smell" with your dynamic method though)

Hope this helps!

Upvotes: 0

Related Questions