laravel paginate passing additional var - laravel

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

Related

How to handle new incoming entries while paging through posts?

Let's assume I have a pagination like this
return App\Post::paginate(1);
After loading this someone creates a new entry in the database for posts. How can i ensure that the "second page" of my pagination does not return the first one again?
Will I always need to send a timestamp with every request to make an additional query before pagination is used?
like
App\Post::where('created_at', '<', $request->input('max_created_at'))->paginate(1);
You can use orderBy. You could do something like that:
// Get the last post (most recent)
return App\Post::orderBy('created_at', 'DESC')->paginate(1);
Or:
// Same as above
return App\Post::orderBy('created_at', 'DESC')->first();
orderBy means all you result are sorted in your query. Hope it will help.
You don't need to pass any timestamp value to verify pagination.
In the controller, in view file you need to pass result of this query.
$result = App\Post::orderBy('created_at', 'desc')->paginate(1);
return view('pagination',compact('result'));
In view file you need below line for pagination work automatically as per Laravel syntax.
{{ $result->links() }}
Visit https://laravel.com/docs/5.2/pagination/ how laravel pagination work.

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

Routing in Codeigniter (:any) gives me strange results

Here i'm using below routing config
$route['controllername/(:any)'] = "controllername/index/$1";
this is working well for me, but i want to use other methods in the same controller.
for that i'm using below route
$route['controllername/search'] = "controllername/search";
this is also working fine, but i want pass parameters to this method.
Here if i'm passing parameters but it is calling index method
i want to use both of above routes, i have tried with below route also but same result
$route['controllername/search/(:any)'] = "controllername/search/$1";
could anyone give any suggestions?
Thankyou!!
Ok.Do it like this for routing multiple functions in same controller :
$route['default_controller'] = "controller_name";
$route['index/(:any)'] = "controller_name/index/$1";
$route['search/(:any)'] = "controller_name/search/$1";
Let me know if you find any issues or doubts.
#user4039421
change your roouting rules position like below
$route['controllername/search/(:any)'] = "controllername/search/$1";
$route['controllername/search'] = "controllername/search";
$route['controllername/(:any)'] = "controllername/index/$1";

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

Get params value from component Joomla 3.0

I have saved value like this in my component table fields params.
unique=1
default_value=Default
validate=Validate
validate_error_msg=Validate error messag
searchable=1
Now i want to get value in my component.So I am passing values in my component's view.html.php
in this way
$params = new JForm($row->params); but its not working.
Now I want to get value so I am taking like this
$this->params->getValue('default_value');
But its not work where as in Joomla 2.5 ,we can get value like this
$this->params->get('default_value');
Try like this
For Ex.
$param = JComponentHelper::getParams('com_users');
$default = $param->get('default_value');
Have you tried to use
$params->get('your_parameter_value_name');
instead of
$this->params->get('your_parameter_value_name');
It should work.

Resources