[英]How to pass a collection or array from blade file to controller in laravel?
$cartItems
contains all the rows of products from database and I am using this inside a blade file. $cartItems
包含数据库中的所有产品行,我在刀片文件中使用它。
I want to pass this $cartItems
back to a controller from this blade file我想将此
$cartItems
从此刀片文件传递回 controller
Note: $cartItems
is from index()
function in a controller like below.注意:
$cartItems
来自index()
function 在 controller 中,如下所示。
$cartItems = DB::table('products')->whereIn('id', $cartItemsArray)->get();
return view('cart.index', compact('cartItems')
Below is my code.下面是我的代码。
index.blade.php
<a href="{{route('cart.checkout',$cartItems)}}" class="site-btn">Proceed to checkout</a>
web.php
Route::get('/cart/checkout/{cartItems}', 'CartController@checkout')->name('cart.checkout')->middleware('auth');
CartController.php
public function checkout($cartItems)
{
dd($cartItems);
return view('cart.checkout');
}
The error I am getting is,我得到的错误是,
Missing required parameters for [Route: cart.checkout] [URI: cart/checkout/{cartItems}].
[Route: cart.checkout] [URI: cart/checkout/{cartItems}] 缺少必需的参数。 (View: E:\github\LARAVEL\Deal-Ocean\resources\views\cart\index.blade.php)
(查看:E:\github\LARAVEL\Deal-Ocean\resources\views\cart\index.blade.php)
You can use a form to send data back to server您可以使用表单将数据发送回服务器
Update your route from get
to post
更新从
get
到post
的路线
Route::post('/cart/checkout', 'CartController@checkout')->name('cart.checkout')->middleware('auth');
Use a form to post data to server.使用表单将数据发布到服务器。 You can pass any additional data along with the request as well.
您也可以将任何附加数据与请求一起传递。
<form method="post" action="/cart/checkout">
@foreach($cartItems as $item)
<input name="cartItems[]" value="{{ $item->id }}"
@endforeach
<button class="site-btn">Proceed to checkout</button>
</form>
And in your controller use Request
to access data并在您的 controller 中使用
Request
访问数据
public function checkout(Request $request)
{
$cartItems = DB::table('products')->whereIn('id', $request->get($cartItems))->get();
dd($cartItems);
return view('cart.checkout');
}
If you want to proceed with the get request you should be able to do as follow如果您想继续获取请求,您应该可以执行以下操作
As $cartItems
is a collection of products.因为
$cartItems
是产品的集合。 So you can send the product ids and query the products using the ids from request.因此,您可以发送产品 id 并使用请求中的 id 查询产品。
<a href="{{ route('cart.checkout', ['cartItems' => $cartItems->pluck('id')->toArray()]) }}"
class="site-btn">Proceed to checkout</a>
Update controller更新 controller
public function checkout(Request $request)
{
$cartItems = DB::table('products')->whereIn('id', $request->get($cartItems))->get();
dd($cartItems);
return view('cart.checkout');
}
Why use the same code logic of the index() method in the checkout method in the CartController.为什么在 CartController 的checkout方法中使用index()方法的相同代码逻辑。
the checkout method will look like this:结帐方法将如下所示:
$cartItems = DB::table('products')->whereIn('id', $cartItemsArray)->get();
return view('cart.checkout', compact('cartItems');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.