Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable with route parameter when form is submitted Laravel 5.2

I have this in my form in the viewpage.php:

<form action="{{ route('test.route'), ['id' => $params_id] }}" method="POST" >

And this in the route.php:

Route::post('/testing/{{id}}',[
    'uses' => 'TestController@testMethod', 
    'as' => 'test.route'
]);

And this is my TestController:

public function avaliarSubordinor(Request $request, $uid){
    return $uid;
}

I get an error which says 'Missing required parameters for[Route: test.route] [URI: testing/{{id}}]. Essentially What i want is to pass a variable to my controller using a route with a parameter when form is submitted..

I dont know if I am doing this properlly..if anyone can help me or point me to an example so i can understand what I am doing wrong..

like image 254
OzmaTis Avatar asked Sep 22 '16 10:09

OzmaTis


People also ask

How do you pass a route parameter in Laravel?

Laravel routes are located in the app/Http/routes. For instance, to pass in a name parameter to a route, it would look like this. Route::get('/params/{name}', function ($name) { return $name }); By convention, the Controller function accepts parameters based on the parameters provided.

What are routes in Laravel explain Route handling with controller and route parameters?

You can define a route to this controller action, as: Route::get('user/{id}', 'UserController@show'); Route::resource: The Route::resource method can be a Restful Controller that produces all the basic routes required for an application and is dealt via controller class.


2 Answers

Laravel 5.2 Missing required parameters for [Route: user.profile] [URI: user/{nickname}/profile]

Using the above link I found a solution.. I changed:

<form action="{{ route('test.route'), ['id' => $params_id] }}" method="POST" >

to

<form action="{{ route('test.route', [$params_id]) }}" method="GET" >

and this:

Route::post('/testing/{{id}}',[
     'uses' => 'TestController@testMethod', 
     'as' => 'test.route'
]);

to

Route::get('/testing/{id}',[
     'uses' => 'TestController@testMethod', 
     'as' => 'test.route'
]);

and for reading value :

if ($request->id) {
}

And It works! But I wonder if anyone else can get a POST version working, or is GET the only way? I dont really know much about GET/POST request, only that it's used in forms and ajax.. Would really like to learn more about HTTP GET/POST, if anyone has anything to add please share!! thanks! Hope this answer will help someone!

like image 169
OzmaTis Avatar answered Oct 11 '22 17:10

OzmaTis


Although it's an old post hopefully it will help others in future. For me in Laravel 5.8 the POST method just worked fine.

HTML form:

<form method="POST" role="form" action="{{route('store_changed_role', [$user_id, $division_id])}}">

Route:

Route::post('/sotre_changed_role/{user_id}/{division_id}', 'Admin\UserController@store_changed_role')->name('store_changed_role');
like image 39
Raihan Ahmed Avatar answered Oct 11 '22 16:10

Raihan Ahmed