Put default value in url - laravel

Hi I am trying to put default id value in my route url in Laravel. How do I achieve it? Please Help me. Thank you.
Route::get('manageattendance/{id}',[App\Http\Controllers\ManageAttendanceController::class, 'index'])-
>name('manageattendance');

replace {id} with {id?} and if you have id as parameter on the index function, set it to the default value like:
public function index($id = 1){
// your code
}

Related

Making Routes in Laravel from URL

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])

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: how to set string as optional in route?

in web.php I have:
Route::get('car/id/{id}/color/{color?}', 'carController#getCar);
But I want the whole part of /color/{color?} to be optional not just the color parameter /{color?}, would you please tell me how to do this?
try this one
Route::get('search/{query?}', 'YourController#method')->where('query','.+')
Better To Used Query String Parameter here(now whole part should be optional by default)...
Route::get('car', 'carController#getCar);
example :
car?id=1&color=red
car?id=2
car
in Controller getCar Method
getCart() {
$id = request()->get('id');
$color = request()->get('color');
}

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.

Zend getParams not returing url params

I have a query:
https://myaddress.ee/admin/usersearchajax?country=EE&query=arno
$this->_request->getParam('query');
returns: NULL
var_dump($_REQUEST['query']);
returns: string(5) "arno"
How to fix this problem? I mean how to get GET values with zend framework?
Lets assume i can't change the query string.
zend version 1.11.11
public function usersearchajaxAction(){
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender( true );
$userService = new Application_Services_User();
$userList = $userService->searchByName($this->_request->getParam('query'));
$this->_helper->json($userService->getArrayForAutoComplete($this->_request->getParam('query'), $userList));
}
I found the problem, there was and override of $_GET variable in my project.
So it is fix now. thank you all for reading and thinking about this
instead of
$this->_request->getParam('query');
use this
$this->_getParam('query');
there is no need to use request action helper in Action to get this params
or try to use this way which uses request action helper
$request = $this->getRequest();
$query = $request->getParam('query');
hope this link will help you
Also you can view all params in a row $this->_request->getParams();
Then find needle param in a row and get using $this->_request->getParam('needle');

Resources