我认为它通常是我尝试使用的关于其参数作为模型对象的文档。saveMany
我有一个名为的模型Set
,hasMany
Equipment
因此,从创建一个集合的 from 中,我提交了许多字段,Equipment
以便保存如下:
//Store method of SetController
$set = new Set();
$set->title = request('title');
$set->eqtype_id = request('eqtype');
$set->product_id = request('product');
$set->parts_count = request('parts_count');
$eq = request('equipments');
$set->equipments()->saveMany(array_map(function($n){return new \App\Equipment(['title' => $n, 'eqtype_id' => request('eqtype')]);}, $eq));
$set->save();
但是,我总是收到关于eqtype_id
没有默认值的错误消息:
SQLSTATE[HY000]: 一般错误: 1364 Field 'eqtype_id' doesn't have a default value (SQL: insert into
equipments
(title
,set_id
,updated_at
,created_at
) 值 (A-1, , 2017-03-18 12:07:30, 2017- 03-18 12:07:30))
我认为,可能request('eqtype')
无法从array_map
回调中访问,因此我将其替换为固定数字,如下所示:
$set->equipments()->saveMany(array_map(function($n){return new \App\Equipment(['title' => $n, 'eqtype_id' => 1]);}, $eq));
但错误也是一样的,这似乎是saveMany
or的问题array_map
。我不知道如何解决这个问题!
注意
该模型Equipment
与两个模型相关,Set
如下Eqytpe
所示:
//Equipment model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* App\Equipment
*
* @mixin \Eloquent
*/
class Equipment extends Model
{
public $table = "equipments";
protected $fillable = ['title'];
public function set()
{
return $this->belongsTo(Set::class);
}
public function eqtype()
{
return $this->belongsTo(Eqtype::class);
}