How to pass Query parameter using route name in laravel? - laravel

I know this one
{{route('editApplication', ['id' => $application->id])}} == /application/edit/{id}
But I need
?? == /application/edit?appId=id
Anyone, please replace the "??" with your answer that helps me out.

It depends how you route looks like:
If you route is:
Route::get('/application/edit/{id}', 'SomeController')->name('editApplication');
when you use
route('editApplication', ['id' => 5])
url will be like this:
/application/edit/5
However all other parameters (that are not in route parameters) will be used as query string so for example:
route('editApplication', ['id' => 5, 'first' => 'foo', 'second' => 'bar'])
will generate url like this:
/application/edit/5?first=foo&second=bar
In case you want to achieve url like this
/application/edit?appId=id
you should define route like this:
Route::get('/application/edit/', 'SomeController')->name('editApplication');
and then when you use
route('editApplication', ['appId' => 5])
you will get
/application/edit?appId=5
url.

route method accepts any set of variable and puts them as query string unless the variable name matches with defined route signature.
Thus, for your case you can do something as follows:
route('editApplication', ['appId' => <your id here>])
You can provide any number of variables with the array.

You need to modift your "editApplication" route without parameter option. If you stil want the parameter just add the following row in your controller and just catch the data by using;
$appId = $request->input('appId');

Related

Laravel validate array values contained in list

I am passing an array in a request to my api. Each value within the array must be within a pre-defined list.
If my list is: name,description,title
name, title //valid
different, title //invalid
I tried array|in:name,description,title but I think for that I can only pass a string.
Can I do this without using a custom rule?
Validate each string in the array:
'values.*' => 'string|in:name,title,description'
Have a look at "Validating Nested Array Input"
If I understand you correctly your validation rules should be (untested)
[
'*.name' => 'required|string',
'*.description' => 'required|string',
]
Maybe you also want to exclude unvalidated Keys

When condition on eloquent with the column values on the table

How can I add in conditional the value of the query i'm using on when function of eloquent laravel?
Here's what I want to happen
$user = User::when('logged' == '3', function ($q){
$q->where('role', 'admin');
)}->get();
As you can see I want to get the role admin only when the column logged is equal to 3 how can I do this one ? thank you.
P.S.
just an example query. thank you.
This is not how when works,
the first condition 'logged' == '3' will always return false;
So it will not use the closure query, directly use get() method, and return all the User's records;
You need to do it like this:
$user = User::where(function($q) {
$q->where(['logged' => 3, 'role' => 'admin'])
})->orWhere('logged', '!=', 3)->get();
You may only want to apply a where statement if a given input value is present on the incoming request. In short you first condition is not base on your table field but base on the request you have. So #TsaiKoga answer's is correct.
The when method only executes the given Closure when the first parameter is true. If the first parameter is false, the Closure will not be executed.
Please read docs here when

Add query parameter to existing parameters with route-helper

I use the route-helper ({{ route('routename') }}) in my Blade template files to filter and/or sort the results of the page.
What is the easiest way to attach a parameter to the previous ones?
As an example:
I visit the page /category1 and see some products. Now I use the sorting which changes the URL to /category1?sort_by=title&sort_order=asc
If I use another filtering now, I would like the parameter to be appended to the current one. So to /category1?sort_by=title&sort_order=asc&filter_by=year&year=2017 but the result is only /category1?filter_by=year&year=2017 .
I create the Urls with the route-helper like:
route('category.show', [$category, 'sort_by' => 'title', 'sort_order' => 'asc'])
route('category.show', [$category, 'filter_by' => 'year', 'year' => $year])
You could probably use something like:
$new_parameters = ['filter_by' => 'year', 'year' => $year];
route('category.show', array_merge([$category], request()->all(), $new_parameters]);
to use all previous parameters and add new ones.
Obviously you might want to use only some of them, then instead of:
request()->all()
you can use:
request()->only('sort_by', 'sort_order', 'filter_by', 'year')

Laravel routing url with variable order of parameters

I am looking at routing to a Controller for GET URL whose parameters can vary in number or the order in which they appear in the URL. There could be many such combinations and I want to invoke the same controller action for all of these URLs
Examples of how my URLs could look like:
Route::get('route1/id/{id}',
'Controller1#controllerAction1');
Route::get('route1/id/{id}/name/{name}',
'Controller1#controllerAction1');
Route::get('route1/name/{name}',
'Controller1#controllerAction1');
Route::get('route1/id/{id}/name/{name}/orderby/{orderby}',
'Controller1#controllerAction1');
Route::get('route1/id/{id}/orderby/{orderby}',
'Controller1#controllerAction1');
Also in the Controller action, I ultimately want to break this query string into an array. For the second example mentioned above, I want the query string id/{id}/name/{name} to be converted to array ('id' => {id}, 'name' => {name})
To invoke the same controller action for all different variations of the URLs, I have the following code in my routes.php:
Route::get('route1{all}', 'Controller1#controllerAction1')->where('all', '.*')
which seems to invoke the "controllerAction1" of Controller1 for the different types of URLs mentioned above.
And in the function controllerAction1, I am doing
$route_input = Route::input('all');
var_dump($route_input);
which prints "/id/1/name/xyz" when I hit http://example.com/laravel/public/route1/id/1/name/xyz
I would like to know if:
Doing Route::get('route1{all}',
'Controller1#controllerAction1')->where('all', '.*') is the right
method to invoke same action for variable combination of get
parameters? Does Laravel offer any function to convert
"/id/1/name/xyz" to array('id' => 1, 'name' => 'xyz') or I need to
write custom function? Is there a better way to achieve my
requirements?
I believe not. Plus, in this way you won't be able to understand which values are being passed.
Even if there is one, I think you don't actually need to pass the array. IMHO, I prefer to keep the items separate, then manipulate them from the controller. This is just my personal suggestion, but if you need an array of data, why don't you use a POST method? (the only right answer, is that you want the users to be able to save the link :P )
The complicated part about your request, is that you want to keep everything under the same controller action, which messes the routes. I would try this (in your routes.php):
Route::pattern('id', '[0-9]+');
Route::pattern('name', '[a-Z]+');
Route::get('route1/{id}/{name?}/{orderby?}', 'Controller1#controllerAction1');
Route::get('route1/{name}/{orderby?}', 'Controller1#controllerAction1');
In this way:
you can have a route with just the ID, where NAME and ORDERBY are optional
if no ID is passed, you can have a route with only NAME, where ORDERBY is optional
Note how this is different from your URLs: it's much more complicated to put the routes as you wrote them id/{id}/name/{name}, than in the way I proposed {id}/{name}. If you need them exactly your way, why don't you call the links passing the variables from the GET function as follows? http://www.yoursite.com/route1?id=xxxx&name=yyyy&orderBy=zzzz
To have the route parameters convert from a set of individual parameters to an array that contains all the parameters in Laravel 5, you can call this from the Controller:
$routeParameters = $this->getRouter()->getCurrentRoute()->parameters()
For the route definition
Route::get('route1/id/{id}/name/{name}', 'Controller1#controllerAction1');
if a user hits the route with the following: /route1/id/2/name/john
$routeParameters would equal
array(id => 2, name => 'john')

Remembering CodeIgniter form_dropdown fields

This works...
form_dropdown('location', $location_options, $this->input->post('location'));
But when I try and use an array to add extra attributes, it stops working... Why is this?
$attributes = array(
'name' => 'location',
'id' => 'location'
);
form_dropdown($attributes, $location_options, $this->input->post('location'));
The name of the dropdown list is included in the array of attributes so i don't see how this is any different to the first example. Whenever the form is posted posted back, it resets to the start.
Can anyone help me out with this?
Thanks
It's just the wrong syntax.
Please have a look at the docu: http://codeigniter.com/user_guide/helpers/form_helper.html
form_dropdown('location', $location_options, $this->input->post('location'), "id='location'");
Your code should look something like the above. And by the way: if you're using the form_validation library you could use set_value instead of $this->input->post ...
$attributes = ' id="bar" class="foo" onChange="some_function();"';
$location_options = array(
'IN' =>'India',
'US' =>'America'
);
form_dropdown('location', $location_options, $this->input->post('location'),$attributes);
Parameters :
1st param will assign to name of the field,
2nd will get your options,
3rd is for default value,
4th one is for extra properties to add like javascript function, id, class ...

Resources