'How to use array as route parameter properly
I have a form with this action:
<form method="POST" action="{{ route('products.create.post.attribute',['product'=>$product->id,'total'=>$total_counts,'array'=>$attribute_ids]) }}">
So basically, $attribute_ids is an array like this:
array:6 [▼
0 => 14
1 => 15
2 => 16
3 => 3
4 => 7
5 => 8
]
And here is the route:
Route::post('/create/product/addAttribute/{product}/{total}/{array}', [ProductController::class, 'postAttribute'])->name('products.create.post.attribute');
Then at the Controller, I set up the method like this:
public function postAttribute(Request $request, Product $product, $total,$array){
But I get this error:
Too few arguments to function ProductController::postAttribute(), 3 passed in C:\projectname\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 4 expected
So what's going wrong here? How can I properly use array as route parameter?
Solution 1:[1]
You can not pass array in the url. You have pass your array data in string format. There is two way to pass data :
- In URL - which you are doing .
- Form data - using inpur field.
You must have to make a string of that array using implode
Either create foreach and inside it create input with name="attribute[]" add id in input field. This way you will get $request->attribute in array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Apurv Bhavsar |
