为什么performInsert()从数组中删除字段?

时间:2017-06-30 11:47:10

标签: laravel laravel-5 eloquent

我不明白为什么performInsert()会删除我的' _logo' Laravel 5.2中的字段。 除了' id',' created_at'和' 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)

更新

我基本上以这种方式处理请求表单中的每个字段:

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());
    }
}

我的模特是:

namespace App;

use Illuminate\Database\Eloquent\Model;

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


}

我的表单是从模型字段开始构建的,所以我将每个字段的表单都放到表单中,然后将它们检索回商店函数

1 个答案:

答案 0 :(得分:0)

查看您无法添加_logo的原因是因为它以_开头且不在可填充数组中。当可填充数组为空时,Laravel将允许任何未受保护且不以_开头的列。

你可以:

  • 从列名称
  • 中删除_
  • 明确表示列是可填写的,即:

    protected $fillable = ['nome', 'cognome', 'tipo', 'pwd', 'telefono', 'indirizzo', 'email', '_logo']; //and any other columns that are allowed (if any)
    
  • 手动将_logo值添加到实例,即$model->_logo = $request['logo'];。 (这可能会导致你的动态方法“代码味道”)

希望这有帮助!