0

我有一个带有此操作的表格:

<form method="POST" action="{{ route('products.create.post.attribute',['product'=>$product->id,'total'=>$total_counts,'array'=>$attribute_ids]) }}">

所以基本上,$attribute_ids是这样的数组:

array:6 [▼
  0 => 14
  1 => 15
  2 => 16
  3 => 3
  4 => 7
  5 => 8
]

这是路线:

Route::post('/create/product/addAttribute/{product}/{total}/{array}', [ProductController::class, 'postAttribute'])->name('products.create.post.attribute');

然后在控制器上,我设置了这样的方法:

public function postAttribute(Request $request, Product $product, $total,$array){

但我得到这个错误:

函数 ProductController::postAttribute() 的参数太少,在第 54 行的 C:\projectname\vendor\laravel\framework\src\Illuminate\Routing\Controller.php 中传递了 3 个,而预期的正好是 4 个

那么这里出了什么问题?如何正确使用数组作为路由参数?

4

2 回答 2

2

发生错误是因为您使用数组作为参数名称。更改参数名称,它应该可以工作。

<form method="POST" action="{{ route('products.create.post.attribute',['product'=>$product->id,'total'=>$total_counts,'myarray'=>$attribute_ids]) }}">

路线

Route::post('/create/product/addAttribute/{product}/{total}/{myarray}', [ProductController::class, 'postAttribute'])->name('products.create.post.attribute');

控制器

public function postAttribute(Request $request, Product $product, $total, $myarray){

于 2022-03-05T08:27:50.627 回答
2

您不能在 url 中传递数组。您已以字符串格式传递数组数据。有两种传递数据的方法:

  1. 在 URL - 你正在做的。
  2. 表单数据- 使用 inpur 字段。

您必须使用该数组创建一个字符串implode

创建 foreach 并在其中创建inputname="attribute[]"在输入字段中添加 id。这样你就会进入$request->attribute数组。

于 2022-03-05T08:51:33.227 回答