这些是我的模特:
订单:
class orders extends Model
{
protected $table = 'orders';
public function getItems()
{
return $this->hasMany('App\order_items');
}
protected $fillable = [
'subtotal','discount_amount','after_discount_price','mechanic_id','owned_cars_id','primary_id','promo_code_string',
'order_status_id'
];
}
订购商品:
class order_items extends Model
{
protected $table = 'order_items';
protected $fillable = [
'order_primary_id','primary_id','service_id','service_name','service_thumbnail','service_orignal_price','discount_amount',
'after_discount_price','service_description','service_classification'
];
}
这是JSON结构
{
"orderID": null,
"PromoId": 0,
"subtotal": 2500,
"discount": 12,
"discountPrice": 3500,
"mechanic_id": null,
"ownedcarId": 1,
"ownedCarServerId": 0,
"order_status_id": 1,
"order_items": [
{
"order_item_server_id": null,
"order_id": null,
"order_primary_id": 1,
"primary_id": 1,
"service_id": 1,
"service_name": "Car Wash",
"service_thumbnail": "asd",
"service_original_price": 2500,
"discount_amount": 20,
"after_discount_price": 20,
"service_description": "description",
"service_classification": 1,
"service_sub_items":0
}
]
}
对于新订单,此代码可以正常工作:
$order = new order();
$order->user_id = 123;
$order->fill($request->all());
$order->save();
有什么方法可以使用$ request-> all()直接在关键字“订购商品”中保存质量可分配数据?或者我必须一个接一个地做?
答案 0 :(得分:0)
你可以尝试:
foreach($request->order_items as $item){
$orderItem = new OrderItem($item);
$orderItem->save();
}
答案 1 :(得分:0)
如果您的请求中有额外的字段,那么这些字段并不属于您的模型。你可以这样做:
$input = $request->only(['username', 'password']);
$input = $request->only('username', 'password');
$input = $request->except(['credit_card']);
$input = $request->except('credit_card');
取自"检索输入数据的一部分"来自https://laravel.com/docs/5.4/requests