如何在 Laravel 中将数据从 1 个视图插入到 2 个表中
Posted
技术标签:
【中文标题】如何在 Laravel 中将数据从 1 个视图插入到 2 个表中【英文标题】:How to insert data from 1 view to 2 tables in Laravel 【发布时间】:2021-02-06 06:13:33 【问题描述】:我正在尝试将结帐视图中的数据插入到我数据库中的 2 个表中。这是我的 CheckoutController:
19 public function index()
20
21 $websites = Website::all();
22 return view('checkout')->with([
23 'websites' => $websites,
24 ]);
25
26
27 /**
28 * Store a newly created resource in storage.
29 *
30 * @param \Illuminate\Http\Request $request
31 * @return \Illuminate\Http\Response
32 */
33 public function store(Request $request)
34
35 // Insert into orders table
36 $data = Order::create([
37 'user_id' => $request->user_id,
38 'date' => $request->date,
39 'order_number' => $request->order_number,
40 'payment_method' => $request->payment_method,
41 'billing_subtotal' => $request->billing_subtotal,
42 'billing_tax' => $request->billing_tax,
43 'billing_total' => $request->billing_total,
44 ]);
45 $order = DB::table('orders')->insert($data);
46
47 //Insert into order_menu table
48 foreach (Cart::content() as $item)
49 $cartitem = OrderMenu::create([
50 'order_id' => $data->id,
51 'menu_id' => $item->model->id,
52 'quantity' => $item->qty,
53 ]);
54 $cart = DB::table('order_menu')->insert($cartitem);
55
56
57 Cart::instance('default')->destroy();
58 return redirect('status');
59
对于 Checkout.blade.php :
<form action=" route('checkout.store') " method="POST" enctype="multipart/form-data">
csrf_field()
<div style="display: grid; grid-template-columns: 1fr 1fr; grid-gap: 100px">
<div class="details">
<div class="billing">
<div class="cart-title"> Detail Pesanan </div>
<hr>
<form class="form-inline">
<div class="mb-3">
<label for="user_id" class="form-label"> Meja </label>
<input type="text" value=" auth()->user()->name " class="form-control" disabled placeholder=" auth()->user()->name ">
</div>
<div class="mb-3">
<label for="payment_method" class="form-label"> Payment </label>
<select name="payment_method" id="payment_method" class="form-control" required>
<option value="" style="font-size: 15px;"> Choose Payment... </option>
<option name="payment_method" value="Pay Now" style="font-size: 15px;"> Pay Now </option>
<option name="payment_method" value="Pay Later" style="font-size: 15px;"> Pay Later </option>
</select>
</div>
<input id="user_id" name="user_id" type="hidden" value=" Auth::user()->id " />
<input id="date" name="date" type="hidden" value=" date('d M Y', strtotime(today())) " />
<input id="order_number" name="order_number" type="hidden" value=" rand(1000,9999) date('Ymd', strtotime(today())) " />
<input id="billing_subtotal" name="billing_subtotal" type="hidden" value=" Cart::subtotal() " />
<input id="billing_tax" name="billing_tax" type="hidden" value=" Cart::tax() " />
<input id="billing_total" name="billing_total" type="hidden" value=" Cart::total() " />
</div>
<div class="cart-buttons" style="margin-top: 50px;">
<a href=" route('cart.index') " class="btn btn-secondary" style="color: white;"> Kembali ke Keranjang </a>
<button type="submit" class="btn btn-danger" style="color: white; background-color: #BB4430"> Buat Pesanan </button>
</div>
</div>
<div class="cart">
<div class="cart-title">
Pesanan Anda
</div>
<div class="cart-table">
<table class="table">
<tbody>
@foreach (Cart::content() as $menu)
<tr>
<td>
<t style="font-weight: bold;"> $menu->model->name </t>
<br>
<d style="color: #acacac;"> </d>
</td>
<td>
$menu->qty
</td>
</tr>
@endforeach
<tr>
<td style="color: #acacac;"> Subtotal </td>
<td style="color: #acacac;"> Rp. Cart::subtotal() </td>
</tr>
<tr>
<td style="color: #acacac;"> Pajak </td>
<td style="color: #acacac;"> Rp. Cart::tax() </td>
</tr>
<tr>
<td> Total </td>
<td> Rp. Cart::total() </td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</form>
但它只向订单表插入数据并得到这个错误:
"传递给 Illuminate\Database\Query\Builder::insert() 的参数 1 必须是数组类型,给定对象,在 D:\xampp\htdocs\mieaceh\app\Http\Controllers\CheckoutController.php 中调用在第 45 行"
而且对于日期输入,数据也无法插入到数据库表中。
我该如何解决这个问题?我是 Laravel 的新手
【问题讨论】:
请在此处添加您的完整控制器功能,包括功能定义行。另外,如果您可以添加表单,那将更有帮助。 @aufa 有用吗? 【参考方案1】:Model::create() 函数在表中创建一个新条目,您不需要通过$order = DB::table('orders')->insert($data);
再次插入它并且
$cart = DB::table('order_menu')->insert($cartitem);
。再加上第二次,它只是您尝试插入表中的前一个插入返回的对象。
// Insert into orders table
$data = Order::create([
'user_id' => $request->user_id,
'date' => date('Y-m-d', $request->date), // Edit
'order_number' => $request->order_number,
'payment_method' => $request->payment_method,
'billing_subtotal' => $request->billing_subtotal,
'billing_tax' => $request->billing_tax,
'billing_total' => $request->billing_total,
]);
$order = DB::table('orders')->insert($data); // Remove this line
//Insert into order_menu table
foreach (Cart::content() as $item)
$cartitem = OrderMenu::create([
'order_id' => $data->id,
'menu_id' => $item->model->id,
'quantity' => $item->qty,
]);
$cart = DB::table('order_menu')->insert($cartitem); // Remove this line
Cart::instance('default')->destroy();
return redirect('status');
【讨论】:
这行得通,但是日期输入呢?dd($request)
在Order::create()
之前执行此操作,并查看日期输入的格式是否为“YYYY-MM-DD”。如果没有,请按照我对答案所做的编辑。 @aufa
我按照你说的做了,但它仍然不会将日期插入数据库以上是关于如何在 Laravel 中将数据从 1 个视图插入到 2 个表中的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Laravel 中将变量从视图传递到控制器而不使用 URL 或表单