Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

The error occurs because you using array as parameter name. Change the parameter name and it should work.

blade

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

route

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

controller

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading