I have a route - let's call it stats. Here is what my routing currently looks like:
Route::get('stats', 'StatsController@index');
Route::get('stats/{query}', 'StatsController@store');
My goal is to show stat data if someone visits /stats and to store stat data if someone visits a URL similar to /stats?name=John&device=Android.
How would I then route if there is a query string attached to my namespace stats?
Something like this?
Route::get('stats/?name=*&device=*', 'StatsController@store');
How to generate route with query string parameter in Laravel 5? Whenever you require to add query string parameter in link url, i mean in route then you generate link with query string parameter using Laravel 5 URL facade. most of new developer doing this way: But you should do it that this way, bellow example you can see how it done.
Laravel default provides redirect () helper. redirect () helper provides method route for redirect named route. In Bellow’s example, the cart () function for the route handler. this function redirects home route with 2 parameters as query string like id and product_id, so URL will be like this way:
When we need to add any query string or parameter in our link url, here i mean that in the route then we generate a link also with the query string parameter by using Laravel URL facade. most of the new developer doing this way: <a href =" { { URL::route ('home') }}.'?id=1&test_id=2'" > Test </a> But we should do this it that way.
The route helper will automatically extract the model's route key: Laravel allows you to easily create "signed" URLs to named routes. These URLs have a "signature" hash appended to the query string which allows Laravel to verify that the URL has not been modified since it was created.
routes.php
Route::get('stats', 'StatsController@index');
StatsController
public function index()
{
if(Input::has('name') and Input::has('device')))
return $this->store();
// Show stat ...
}
public function store()
{
$input = Input::only('name', 'device');
// Store stat ...
}
although it seems a pefect scenario for a RESTFUL controller. Whoever is sending the input should do it with a POST request
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With