laravel - 无法尝试以与附加字段的一对多关系插入数据
问题描述
我正在尝试从父关系创建记录。
我的模型
use Illuminate\Database\Eloquent\Model;
class Cheque extends Model
{
protected $fillable = [
"numero",
"banco",
"factura_id",
];
public function factura()
{
return $this->belongsTo('App\Factura');
}
}
class Factura extends Model
{
protected $fillable = [
'cliente_id',
'cotizacion_id',
'sub_total',
'porcentaje_descuento',
'descuento_total',
'total',
'iva',
];
public function cheque()
{
return $this->hasMany('App\Cheque');
}
这是我在 FacturaController 中的代码
$factura = Factura::create($request->all());
$factura->cheque()->create($request->cheque);
请求:
{
"cliente_id" : "3",
"nombre" : "gustavo",
"sub_total" : 20000.50,
"porcentaje_descuento" : 20,
"descuento_total" : 50,
"total" : 100,
"iva" : 12,
"cheque" : {
"1" : {
"numero" : "25525886",
"banco" : "banesco"
}
}
}
“支票”已创建,但“numero”和“banco”字段留空。
我究竟做错了什么?
提前致谢
解决方案
$factura = Factura::create($request->all());
$cheque = Cheque::create($request->only(['input', 'inputenter code here']);
$cheque = factura_id = $factura-id;
$cheque->save();
是这样的
推荐阅读
- json - URL 中的变音符号失败 Codable (Swift) - 怎么办?
- android - 屏幕旋转后片段中的 Recyclerview 未加载
- ionic3 - 没有在离子中获取变量的更新值
- python - 部署 Scrapy 时出现 404 错误
- docker - Docker覆盖DNS无法解析容器名称
- asp.net - 当页面重定向到asp.net中的同一页面时保留文本框值
- angular - mydaterangepicker angular4 如何与输入字段绑定?
- add-in - Enterprise Architect 加载项 ConstLayOutStyles 用法
- vba - ActiveDocument.Paragraphs(1).Range.Font.color 在 word 2013 中不可用?
- rest - Backbone.js - 我应该使用模型还是集合来完成这项工作?