我正在尝试使用laravel创建和编辑订单,但是当我调用create函数时,出现错误(找不到基本表或视图:1146表'easyres.order_products),然后如果我将表名称更改为order_products,具有相同的错误,但带有(order_product)
订购产品型号:
protected $fillable = [
'count' , 'price'
];
订单模型:
public function products(){
return $this->belongsToMany('App\Product')->withPivot('count')->withPivot('price');
}
产品型号:
public function order(){
return $this->belongsToMany('App\Order');
}
创建功能:
public function createOrder(Request $request)
{
$order = new Order;
$order->status = $request->status;
$order->user_id = auth()->user()->id;
$order->save();
$product_ids = $request->products;
foreach ($product_ids as $index => $productId) {
$id = $productId['product_id'];
$count = $productId['count'];
$product = Product ::find($id);
$orderProduct = new Orderproduct;
$orderProduct->product_id = $id;
$orderProduct->order_id = $order->id;
$orderProduct->count = $count;
$orderProduct->price = $product->product_price;
$orderProduct->save();
}
return response()->json([
'message' => 'Success'
],200);
}
编辑功能:
public function EditOrder(Request $request , $id){
$order = Order::find($id);
$order->status = $request->status;
$order->user_id = $request->user_id;
$order->update();
$product_ids = $request->products;
$selectedItems = [];
foreach ( $product_ids as $key => $productId) {
$id1 = $productId['product_id'];
$product = Product ::find($id);
$count = $productId['count'];
$selectedItems[$id1] = ['product_id' => $productId['product_id'] , 'count' => $productId['count'] , 'price' => $product->product_price];
$order->products()->updateExistingPivot($id1, $selectedItems[$id1]);
}
return response()->json([
'message' => 'Success'
],200);
}
我想确定order_product表的名称
答案 0 :(得分:1)
在您的/[;,]|\\e/
模型中,将表名称设置为属性
OrderProduct
因为雄辩的人表名采用复数形式