What to do when laravel routing is duplicated in laravel - laravel

I have added two routes, as described below.
I want the following behavior.
/api/user/1 or /api/user/2 or . . . → UserController#show is excute
/api/user/auth → UserController#showAuthUser is excute
However, when call /api/user/auth, UserController#show is excuted.
How do I get what I want?
Route::get('user/{user_id}', 'Api\UserController#show');
Route::get('user/auth', 'Api\UserController#showAuthUser');

Put your routes with fixed "parameters" first :
Route::get('user/auth', 'Api\UserController#showAuthUser');
Route::get('user/{user_id}', 'Api\UserController#show');
If you put {user_id} first, Laravel will put "auth" in your "user_id" variable

Switch the order of your routes
Route::get('user/auth', 'Api\UserController#showAuthUser');
Route::get('user/{user_id}', 'Api\UserController#show');
Otherwise it expects 'auth' to be a 'user_id'

Related

Laravel validation if both field are empty then error . But if one is null then other is required

I am trying to make a rule if one is empty or not present then other is required but if both are not present then it give error.
I have used this but if both are empty then it not give any error
'inventory_storage_container_id' => 'required_if:inventory_storage_space_id,=,null|integer|exists:inventory_storage_containers,id',
'inventory_storage_space_id' => 'required_if:inventory_storage_container_id,=,null|integer|exists:inventory_storage_spaces,id',
What i have to do ? This is a different scenario if i not include both field in request body it not give any error but i need an error if both are empty . This topic is not duplicate .
try required_without
'inventory_storage_container_id' => 'required_without:inventory_storage_space_id|integer|exists:inventory_storage_containers,id',
'inventory_storage_space_id' => 'required_without:inventory_storage_container_id|integer|exists:inventory_storage_spaces,id',

Laravel get Route parameters outside controller

I have defined a dummy route like this:
Route::get('sth/{v1}/{v2}' , [
'uses'=>'SthController#sth',
]) ;
how can I get the value of v1 and v2, outside controllers?
use this code
$current_params = Route::current()->parameters();
dd($current_params->v1) ;
You can get the values of v1 and v2 anywhere like this:
request()->v1;
request()->v2;
In Laravel 5.6 for me it was:
Route::current()->parameters['v1']
Route::current()->parameters['v2']
etc...
You can put the data in session in controller when pass, then from anywhere you can get your desire data,
Session::put('v1');
Session::put('v2');
now anywhere you can access like:
Session::get('v1')
Session::get('v2')
if you need to delete session data just use
Session::forget('v1')
Session::forget('v2')
You can use the laravel helper named: request()
$value = request('key', $default);
For your route you can use
$v1 = request('v1','default data');
$v2 = request('v2','default data');
Laravel documentation: request helper laravel
haven't tried but think its Route::current(), use from anywhere to access the parameters
$currentParams = Route::current()->parameters();
This can be alternative way:
Route::getCurrentRoute()->getParameter('v1')

laravel paginate passing additional var

let say i have url like this
http://localhost:8080/myweb/listproduct?idcategory=3
but when i used laravel pagination link, my passing variabel by url disappear like this
http://localhost:8080/myweb/listproduct?page=2
i want laravel paginate to pass my url variable too like this
http://localhost:8080/myweb/listproduct?idcategory=3&page2
so far i already try to set baseurl like this
$listproduct = DB::table('product')->paginate(15);
$users->setBaseUrl('listproduct?idcategory=3');
but the result is like this (? not &)
http://localhost:8080/myweb/listproduct?idcategory=3?page2
i used laravel 4.2
you can use setPath()
$users->setPath('listproduct?idcategory=3');
or if you only want to add to the query string you may use
$users->appends(['idcategory' => '3'])
You can use this method to append more arguments to your pagination link:
$listproduct->appends(['idcategory' => $id])->links();

Laravel - Get route by a route name

route('products.create') returns full path like http://myapp.dev/products/create.
How do I get the actual route? Only this -> products/create?
There is no such functionality provided by Laravel at this point. However, you may try this:
$extra = URL::to('/');
$actual = route('products.create');
str_replace($extra, '', $actual);
That will remove the unnecessary base URL from your route URL.

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

Resources