Not able to get Route parameter - Laravel 6 - laravel

I have tried multiple solution but nothing worked yet, i am trying to get route Parameter in controller that was passed from a view.
Here is how i have created the route:
Route::get('addOptions/{questionId}', 'QuestionController#addOptions')->name('addOptions');
Here is how i am passing parameter to route from view:
Add Options
And here is what i am trying to get in controller but it's returning empty array:
public function addOptions(Request $request)
{
$allParameters = $request->input(); //not working
//$allParameters = $request->all(); //not working
//$allParameters = Input::all(); //not working
return $allParameters;
}
It returns empty array [] like this.
EDIT: But url at route addOptions look like this http://127.0.0.1:8000/admin/addOptions/4 in which 4 is questionId which means parameter is being passed but not retrieved.
What am I doing wrong here? Please guide, Thanks.

You should be passing the route like this:
Add Options
as for Laravel docs. the route params are passed an array with the key referencing the param
$url = route('profile', ['id' => 1]);
To retrieve the data in your controller, you should use:
$request->route()->paremeters()
or
$request->route('parameter_name')

If you want to pass the parameter
Add Options

I think your function parameters are wrong
You are passing question id from Route So your function should be like
public function addOptions($questionId)
{
$allParameters = $questionId; // you question ID passed throught Route
return $allParameters;
}

Related

Stumpped trying to pass two parameters from view form to controller

I have a Laravel 6 app and am trying to pass two parameters from my view's form to my controller via a resource route. I can pass one, no problem, but passing two gives the same error:
Too few arguments to function App\Http\Controllers\Admin\SubscriptionsController::update(), 1 passed and exactly 2 expected
I've tried many different arrangements suggested from other posts but none bring the desired result.
Here's my route:
Route::resource('subscriptions', 'Admin\SubscriptionsController')
Here's my form in my view:
{{ Form::open(['route' => ['admin.subscriptions.update', $plan->id, $coupon_code], 'method' => 'PUT', 'id' => 'role-' . $plan->id, $coupon_code]) }}
Coupon Code: <input type="text" name="coupon_code">
{{ Form::close() }}
Here's my controller. It doesn't reach the dd() test.
public function update($id, $coupon_code)
{
dd($coupon_code);
...
In the error Whoops! page, I can see the POST DATA that $coupon_code is being sent over.
However, if I remove the $coupon_code parameter from the controller (leaving public function update($id) ) it functions fine passing $id from form to controller, but I don't get the $coupon_code data I need to process. Adding the second parameter bombs it.
Any suggestions are very welcome.
As you are using the resource controller, it won't allow you to pass additional fields directly to its update() route. Instead, you have to override the update route.
Here is how you can do it, Immediately below your resource.
You can add a new route as below:
// web.php
Route::resource('subscriptions', 'Admin\SubscriptionsController')
// Make sure you add name when you are overriding the route
Route::put('subscriptions/{subscription}/{coupon_code?}', 'Admin\SubscriptionsController#update'])->name('admin.subscriptions.update');
I believe you won't be sending the coupon code every time so you can add {coupon_code?} which becomes optional.
In you controller's update() method, make the coupon_code optional
public function update($id, $coupon_code = null)
{
...
}
You can not add a new param for Route::resource. If you really want to take 2 params, you should create a new route.
for an example:
Route::resource('subscriptions', 'Admin\SubscriptionsController')->except('update');
Route::put('/subscriptions/{id}/{coupon_code}', 'Admin\SubscriptionsController#update')->name('subscriptions.update');
But I think it's better not using method params. Why not just using input form?
so we can process the coupon code like this:
request()->coupon_code;

How to pass request parameter with post method from a controller to another controller method in Laravel?

I want to call the method of B controller from Acontroller.So I used this method
return redirect()->action('UserController#subscribe()');
But here I am facing an issue here. I want to pass below param as in request body to above subscribe()
$package_id = session('package_id');
$package_type = session('package_type');
Please help me out how can I pass above param in request in laravel
If you need to call one controller from another, it seems like you have not so good architecture, and you have to refactor your code.
But, if you anyway want to do it, you can do it like that:
app('App\Http\Controllers\UserController')->subscribe($package_id, $package_type);
Controller 1
public function method1()
{
$package_id = session('package_id');
$package_type = session('package_type');
return app('App\Http\Controllers\Controller2')->method2($package_id, $package_type);
}
Controller 2
public function method2($package_id, $package_type)
{
//
}
You can append them to the query string.
return redirect()->action('UserController#subscribe', [
'package_id' => $package_id,
'package_type' => $package_type,
]);
You can call the controller.
app(UserController::class)->subscribe();

Laravel 5.7 ApiResource GET params empty

I use Laravel 5.7 for my JSON API web application.
In my routes/api.php file, I created the following route :
Route::apiResource('my_resource', 'API\Resource')->except(['delete']);
I added the corresponding controller and methods (index, show,...) and everythink work perfectly. My issue is the following : I would like to add optional GET params like this :
http://a.x.y.z/my_resource?param=hello&param2=...
And for instance being able to retrieve 'hello' in my index() method. However, when I print the value of $request->input('param'), it's empty. I just don't get anything.
Yet, if I create a route like this, with an optional parameter:
Route::get('/my_resource/{param?}', 'API\Resource');
I'm able to get the parameter value in my controller method.
Here is my index method :
class Resource extends Controller {
public function index(Request $request)
{
print($request->input('param'));
// ...
}
// ...
}
Am I missing something ? I'm still new in Laravel maybe I missed something in the documentation.
Thanking you in advance,
You can use:
$request->route("param");

Missing argument 1 for App\Http\Controllers\ProductsController::edit()

Im working on editing of an already saved product and i am getting the following error message on my browser
ErrorException in ProductsController.php line 88:
Missing argument 1 for App\Http\Controllers\ProductsController::edit()
in ProductsController.php line 88
My route controller is as below:
Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
The function is as below
public function edit($id)
{
//find the post in the db and sav it as a variable
$product = Products:: findOrFail($id);
//return view and pass in the var previously created
return view('/productsedit')->withProducts($product);
}
Where iam i going wrong
In your edit method on your ProductsController you require a parameter ($id), but you don't have that value in your route . Which is what this error is saying.
[Missing argument 1 for
App\Http\Controllers\ProductsController::edit()]
Your route:
Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
Will have to change to something like this:
Route::get('products/{$id}/edit', 'ProductsController#edit');
When calling the route in your view it will have to look something like this:
'products/{{$product->id}}/edit'
Extra:
You might want to take a look at Resource controllers since you are not really following restfull practices when it comes to your routes
https://laravel.com/docs/5.4/controllers#resource-controllers
your should give route id
Route::get('productsedit/{id}', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
and in your function should like this
`public function edit($id = null)
{
//find the post in the db and sav it as a variable
$product = Products:: findOrFail($id);
//return view and pass in the var previously created
return view('/productsedit')->withProducts($product);
}`
hope this work !!!

Routing with parameters - Laravel

I'm trying to craft a route for a controller that will submit some data to a database. My URL is as follows:
http://example.co.uk/posts/5/edit?type=job
I've tried
Route::post('/posts/{id}/edit?type={role}', 'PostsContoller#store');
but am unsure if this will fly?
Don't add parameters in your route:
Route::post('/posts/{id}/edit', 'PostsContoller#store');
In your controller, just check if parameter exist:
$type = Input::has('type') ? Input::get('type') : null;
Don't worry about HTTP verb, as Input access for all verbs (POST,GET,PUT,DELETE...).
Edit
As pointed out by #Antoine, you can simply specify the default value in the get method
$type = Input::get('type', null);
I don't think that this is the right way to do it.
First way
If you change your route to
Route::post('/posts/{id}/edit/{role?}', 'PostsContoller#store');
You will then call the URL: GET posts/42/edit/job.
your store function in PostsController will be:
public function store($id, $role = null)
{
// some code
}
Second way
You can use another route like:
Route::post('/posts/{id}/edit', 'PostsContoller#store');
You will then call the URL: GET posts/42/edit?type=job
And you can get the type in your store function in PostsController:
public function store($id)
{
// $role will be null if type is not in the URL
$role = Input::get('type', null);
// additional code
}
I would personally go for the second way.

Resources