What is the simplest way to add a query string to URL in Laravel? Let's say I have a route resources like this
Route::get('/test', function() {
});
Are there just some parameters I can pass through to make the url look like this /test?foo=bar
I'm super new to Laravel and I'm really not looking for something fancy. Thanks!
The simplest way to do this is to simply add the query string in to the url exactly as you have suggested www.example.com/test?foo=bar
. In an example:
Route::get('test', function(){
return Input::get("foo");
});
This would return bar
.
Simply use Input::get()
to retrieve the parameter from the URL.
You can extend this out as much as you want; www.exmaple.com/test?foo=bar&day=Monday&name=John
and retrieve them all if you needed to using Input::all()
. Printing this out would return:
Array
(
[foo] => bar
[day] => Monday
[name] => John
)
More on the Input
function here: https://laravel.com/api/5.1/Illuminate/Support/Facades/Input.html#method_get
Use /test/{foo}
for compulsory and /test/{foo?}
for optional
Route::get('/test/{foo}', function($foo) {
return "foo value is : ".$foo;
});
You get query string values from the request object:
Route::get('test', function () {
$foo = request('foo'); // $foo is now 'bar' or null
});