Getting GET "?" Variable in Laravel
Take a look at the $_GET and $_REQUEST superglobals. Something like the following would work for your example:
$start = $_GET['start'];$limit = $_GET['limit'];
EDIT
According to this post in the laravel forums, you need to use Input::get()
, e.g.,
$start = Input::get('start');$limit = Input::get('limit');
See also: http://laravel.com/docs/input#input
I haven't tested on other Laravel versions but on 5.3-5.8 you reference the query parameter as if it were a member of the Request
class
.
1. Url
http://example.com/path?page=2
2. In a route callback or controller action using magic method Request::__get()
Route::get('/path', function(Request $request){ dd($request->page);}); //or in your controllerpublic function foo(Request $request){ dd($request->page);}//NOTE: If you are wondering where the request instance is coming from, Laravel automatically injects the request instance from the IOC container//output"2"
3. Default values
We can also pass in a default value which is returned if a parameter doesn't exist. It's much cleaner than a ternary expression that you'd normally use with the request globals
//wrong way to do it in Laravel $page = isset($_POST['page']) ? $_POST['page'] : 1; //do this instead $request->get('page', 1); //returns page 1 if there is no page //NOTE: This behaves like $_REQUEST array. It looks in both the //request body and the query string $request->input('page', 1);
4. Using request function
$page = request('page', 1);//returns page 1 if there is no page parameter in the query string//it is the equivalent of$page = 1; if(!empty($_GET['page']) $page = $_GET['page'];
The default parameter is optional therefore one can omit it
5. Using Request::query()
While the input method retrieves values from entire request payload (including the query string), the query method will only retrieve values from the query string
//this is the equivalent of retrieving the parameter//from the $_GET global array$page = $request->query('page');//with a default$page = $request->query('page', 1);
6. Using the Request facade
$page = Request::get('page');//with a default value$page = Request::get('page', 1);
You can read more in the official documentation https://laravel.com/docs/5.8/requests
We have similar situation right now and as of this answer, I am using laravel 5.6 release.
I will not use your example in the question but mine, because it's related though.
I have route like this:
Route::name('your.name.here')->get('/your/uri', 'YourController@someMethod');
Then in your controller method, make sure you include
use Illuminate\Http\Request;
and this should be above your controller, most likely a default, if generated using php artisan
, now to get variable from the url it should look like this:
public function someMethod(Request $request) { $foo = $request->input("start"); $bar = $request->input("limit"); // some codes here }
Regardless of the HTTP verb, the input() method may be used to retrieve user input.
https://laravel.com/docs/5.6/requests#retrieving-input
Hope this help.