【问题标题】:Why performInsert() removes field from array?为什么 performInsert() 从数组中删除字段?
【发布时间】:2017-12-04 08:53:41
【问题描述】:

我不明白为什么 performInsert() 会在 Laravel 5.2 中删除我的 '_logo' 字段。 我的模型除了“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)

更新

我基本上是这样处理requests Form中的每一个字段的:

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'];


}

我的表单是从模型字段开始构建的,因此我将每个字段都提供给表单,然后将它们检索回存储函数

【问题讨论】:

  • 你能显示模型的代码吗?
  • @bluemoon 已编辑
  • @RossWilson 已编辑
  • 也许是下划线作为第一个字符的问题?

标签: laravel laravel-5 eloquent


【解决方案1】:

查看无法添加_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'];。 (不过,这可能会导致您的动态方法出现“代码异味”)

希望这会有所帮助!

【讨论】:

  • 是的!但也不例外!
猜你喜欢
  • 2016-02-09
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
相关资源
最近更新 更多