Making Routes in Laravel from URL - laravel

If our URL is http://127.0.0.1:8000/student/submit-details/1234 then its Route will be:
Route::get('student/submit-details/{id}',
'studentController#submitDetails')->name('submitDetails');
What will be the route if the URL is following?
http://127.0.0.1:8000/student/submit-details?code=1234
I'm using the following Route, but it is not picking it and not working. Does anyone know what will be its Route? I went through the documentation and found no help there.
Route::get('student/submit-details?code={id}', 'MyController#submitDetails');

Your route should be look like as:
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
http://127.0.0.1:8000/student/submit-details?code=1234
in above URL string after the question mark is query parameter and get the value of the query parameter in the controller you should use $_GET:
$_GET['code']

The placeholder parameters for routes are only specified for Route parameters but rather for query parameters. The Route should be only
Route::get('student/submit-details', 'MyController#submitDetails');
You can access the value in the controller via Request instance
public function submitDetails(Request $request) {
dd($request->code);
}

Try this
http://127.0.0.1:8000/student/submit-details?code=1234
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');

You have to use get method
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');

In Laravel if you want to pass data with GET method :
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
It will give you output like this :
http://127.0.0.1:8000/student/submit-details?code=1234
If you have multiple parameter, it will like :
http://127.0.0.1:8000/student/submit-details?code=1234&code2=5678
You can access the parameter from controller like this :
public function edit(Request $request){
$code = $request->input('code');
dd($code); // 1234
}
Take a look at the $_GET and $_REQUEST superglobals.

If you want route
http://127.0.0.1:8000/student/submit-details?code=1234
Route route will be
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
And usage
route('submitBankDetails', ['code' => 1234])

Related

Not able to get Route parameter - Laravel 6

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;
}

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");

How to set a route parameter default value from request url in laravel

I have this routing settings:
Route::prefix('admin/{storeId}')->group(function ($storeId) {
Route::get('/', 'DashboardController#index');
Route::get('/products', 'ProductsController#index');
Route::get('/orders', 'OrdersController#index');
});
so if I am generating a url using the 'action' helper, then I don't have to provide the storeId explictly.
{{ action('DashboardController#index') }}
I want storeId to be set automatically from the request URL if provided.
maybe something like this.
Route::prefix('admin/{storeId}')->group(function ($storeId) {
Route::get('/', 'DashboardController#index');
Route::get('/products', 'ProductsController#index');
Route::get('/orders', 'OrdersController#index');
})->defaults('storeId', $request->storeId);
The docs mention default parameter though in regards to the route helper (should work with all the url generating helpers):
"So, you may use the URL::defaults method to define a default value for this parameter that will always be applied during the current request. You may wish to call this method from a route middleware so that you have access to the current request"
"Once the default value for the ... parameter has been set, you are no longer required to pass its value when generating URLs via the route helper."
Laravel 5.6 Docs - Url Generation - Default Values
In my Laravel 9 project I am doing it like this:
web.php
Route::get('/world', [NewsController::class, 'section'])->defaults('category', "world");
NewsController.php
public function section($category){}
Laravel works exactly in the way you described.
You can access storeId in your controller method
class DashboardController extends Controller {
public function index($storeId) {
dd($storeId);
}
}
http://localhost/admin/20 will print "20"

Laravel routing hide path

I have a route like this
http://localhost:8000/produk/slug-product
I want to my url like this, remove produk in url
http://localhost:8000/slug-product
What should I do ?
Do not use .htaccess to handle this. Define route for slugs without any segment at the end of the routes list in your app:
Route::get('{slug}', 'FrontController#getBySlug');
So, all requests which are not related to any of the routes, will go to the getBySlug method:
public function getBySlug($slug)
{
$data = Model::findBySlug($slug);
....
}

Laravel 5.2 HTTPNOTFOUNDEXCEPTION

I cant figure out what is causing this error. I have checked to see if the parameters are correct and they seem to be. Also if anybody has an alternative way to get all the parameters in the route rather than listing them all please do tell. I can't find another way.
public function savePaymentDetails(Request $request, $code, $message, $mPAN, $type, $exp, $name, $TxnGUID,
$ApprovalCode, $CVVMatch, $GT_MID, $GT_TRANS_ID, $GT_Val_Code, $ProcTxnID,
$session, $card_brand_selected, $CRE_Verbose_Request, $CRESecureID,
$trans_type, $content_template_url, $allowed_types, $order_desc, $sess_id,
$sess_name, $return_url, $total_amt, $submit, $ip_address, $customer_lastname, $customer_firstname)
{
$code2 = $request->get('code');
echo $code2;
echo $code;
}
The route
Route::get('return/{code}/{message}/{mPAN}/{type}/{exp}/{name}/{TxnGUID}/{ApprovalCode}/{CVVMatch}/{GT_MID}/{GT_Trans_Id
}/{GT_Val_Code}/{ProcTxnID}/{session}/{card_brand_selected}/{CRE_Verbose_Request}/{CRESecureID }/{trans_type}/{content_template_url}/{allowed_types}/{order_desc}/{sess_id}/{sess_name}/{return_url}/{total_amt
}/{submit}/{ip_address}/{customer_lastname}/{customer_firstname}', 'PaymentController#savePaymentDetails');
Here are the parameters returned by the URL that I need to get
/return?code=000&message=Success&mPAN=XXXXXXXXXXXX1111&type=Visa&exp=1218&name=test+visa&TxnGUID=6041323& ApprovalCode=VI0151&CVVMatch=M&GT_MID=672840408068703&GT_Trans_Id=016142173277748&GT_Val_Code=AACA&ProcTxnID=6041323&session=e91dd8af53j35k072s0bubjtn7&card_brand_selected=Visa&CRE_Verbose_Request=1&CRESecureID=gt153545888233SB&trans_type=+2&content_template_url=https%3A%2F%2Fexample.com%2Fpublic%2Ftemplate&allowed_types=Visa|MasterCard|American+Express&order_desc=6&sess_id=e91dd8af53j35k072s0bubjtn7&sess_name=session&return_url=https%3A%2F%2Fexample.com%2Fpublic%2Freturn&total_amt=1.51&submit=submit&ip_address=10.108.231.98&customer_lastname=visa&customer_firstname=test
The route rule you define actually match the url like this
code/message/mPAN/type/exp/name/TxnGUID/ApprovalCode/CVVMatch/GT_MID/GT_Val_Code/ProcTxnID/session/card_brand_selected/CRE_Verbose_Request/trans_type/content_template_url/allowed_types/order_desc/sess_id/sess_name/return_url/submit/ip_address/customer_lastname/customer_firstname
instead of
return?code=blab&blablabal=blab
You url dismatch any route you define , so a not found exception was thrown .
If you are trying to get all url parameters you can just write your route like that .
Route::get('return', 'PaymentController#savePaymentDetails');
And get parameters in your controller :
$parameters = $requests->all();
What's more , if you are trying to save something to your database , you'd better use the post method , you can find more When should i use post or get.

Resources