首页 > 解决方案 > 无法尝试以与附加字段的一对多关系插入数据

问题描述

我正在尝试从父关系创建记录。

我的模型

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”字段留空。

我究竟做错了什么?

提前致谢

标签: laraveleloquentlaravel-5.2

解决方案


    $factura = Factura::create($request->all());
$cheque = Cheque::create($request->only(['input', 'inputenter code here']);
$cheque = factura_id = $factura-id;
$cheque->save();

是这样的


推荐阅读