无法尝试在与其他字段的一对多关系中插入数据
Fail to try insert data in a one-to-many relationship with additional fields
我正在尝试从父关系创建记录。
我的模特
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"
}
}
}
已创建 'cheque',但字段 'numero' 和 'banco' 留空。
我做错了什么?
提前致谢
$factura->cheque($factura_id)->create($request->cheque);
试试这个。
$factura = Factura::create($request->all());
$cheque = Cheque::create($request->only(['input', 'inputenter code here']);
$cheque = factura_id = $factura-id;
$cheque->save();
是这样的
我正在尝试从父关系创建记录。
我的模特
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"
}
}
}
已创建 'cheque',但字段 'numero' 和 'banco' 留空。
我做错了什么?
提前致谢
$factura->cheque($factura_id)->create($request->cheque);
试试这个。
$factura = Factura::create($request->all());
$cheque = Cheque::create($request->only(['input', 'inputenter code here']);
$cheque = factura_id = $factura-id;
$cheque->save();
是这样的