I am using below code to insert record in Database using API.
我正在使用下面的代码使用API在数据库中插入记录。
public function table_order_bill( $table_id, Request $request )
{
$table_orders = RestaurantTableOrder::where('table_id',$table_id)->where( 'restaurant_id', $request->vendor->restaurants[0]->id )->first();
if($table_orders) {
$table_order['restaurant_id'] = $table_orders->restaurant_id; // I have Restaurant ID here.
$table_order['table_id'] = $table_orders->table_id;
$table_order['order_amount'] = $table_orders->order_amount;
$table_order['restaurant_discount_amount'] = $table_orders->restaurant_discount_amount;
$table_order['total_tax_amount'] = $table_orders->total_tax_amount;
$table_order['order_status'] = $table_orders->order_status;
//return response()->json( ['restaurant_id' => $table_orders->restaurant_id ]);
$order = new Order;
$order->fill($table_order);
$result = $order->save();
I am getting error Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1364 Field 'restaurant_id' doesn't have a default value (SQL: insert into
orders (
order_status,
updated_at,
created_at) values (0, 2023-09-10 11:07:49, 2023-09-10 11:07:49)) in file
.
我收到Error Lighmate\Database\QueryException:SQLSTATE[HY000]:一般错误:1364字段‘restaurant_id’在文件中没有默认值(SQL:INSERT INTO ORDERS(ORDER_STATUS,UPDATED_AT,CREATED_AT)值(0,2023-09-10 11:07:49,2023-09-10 11:07:49)。
If I use this code return response()->json( $table_order );
, I am getting below output.
如果我使用此代码返回Response()->json($TABLE_ORDER);,则会得到低于输出的结果。
{
"restaurant_id": 51,
"table_id": 5,
"order_amount": "2.34",
"restaurant_discount_amount": "0.00",
"total_tax_amount": "0.39",
"order_status": 0
}
If I am using this code.
如果我使用这个代码。
public function table_order_bill( $table_id, Request $request )
{
$table_orders = RestaurantTableOrder::where('table_id',$table_id)->where( 'restaurant_id', $request->vendor->restaurants[0]->id )->first();
if($table_orders) {
$table_order['restaurant_id'] = $table_orders->restaurant_id;
$table_order['table_id'] = $table_orders->table_id;
$table_order['order_amount'] = $table_orders->order_amount;
$table_order['restaurant_discount_amount'] = $table_orders->restaurant_discount_amount;
$table_order['total_tax_amount'] = $table_orders->total_tax_amount;
$table_order['order_status'] = $table_orders->order_status;
return response()->json( ['restaurant_id' => $table_orders->restaurant_id ]);
I am getting this result.
我得到了这个结果。
更多回答
you assign $table_orders
and then use $table_order
. Is that just in the question or is that the problem?
分配$TABLE_ORDERS,然后使用$TABLE_ORDER。这只是问题中的问题,还是问题所在?
Do you have 'restaurant_id' in your fillable property array of Order model ?
您的可填充属性订单数组模型中是否有‘restaurant_id’?
protected $fillable = ['restaurant_id', ...];
Or manually mass assign
或手动批量分配
$order = new Order();
$order->restaurant_id = $table_orders->restaurant_id;
...
$order->save();
更多回答
我是一名优秀的程序员,十分优秀!