One thing that crossed my mind is this:
You can create a custom helper to add the parameter to the routes or you can use your custom UrlGenerator.
Custom helper
For adding a custom helper, ideally you should add a custom helper file in your project's composer.json file for the sake o simplicity, and to test it out, you can define the custom helper on your './routes/web.php` file.
Add this function:
function custom_route($name, $parameters = [], $absolute = true)
{
if (request()->query->get('demo') === '1') {
$parameters = Arr::wrap($parameters);
Arr::set($parameters, 'demo', 1);
}
return route($name, $parameters, $absolute);
}
Them replace every occurrence of the route() helper to this custom_route().
Custom UrlGenerator
This one is a bit tricker. Internally the route(...) helper uses the UrlGenerator's route(...) method, so you can create a class that extends from UrlGenerator and replace it on Laravel's Service Container.
1 - Create a class that extends the UrlGenerator, here I am placing in the ./app folder:
<?php
namespace App;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Arr;
class MyUrlGenerator extends UrlGenerator
{
public function toRoute($route, $parameters, $absolute)
{
if ($this->request->query->get('demo') === '1') {
$parameters = Arr::wrap($parameters);
Arr::set($parameters, 'demo', 1);
}
return parent::toRoute($route, $parameters, $absolute);
}
}
2 - To replace the UrlGenerator with this custom class in the Service Container, add the code below in your ./app/Providers/AppServiceProvider.php inside its register method:
$this->app->bind('url', function ($app) {
return new MyUrlGenerator(
$app['router']->getRoutes(),
$app['request'],
$app['config']['app.asset_url']
);
});
Now every time you use the route(...) helper to generate a URL it will check for the query string.
===
IMPORTANT The big downside of both approach is to keep track of related changes in the framework code and update your custom helper or UrlGenerator to match those changes.
Other downside is the added overhead of checking the request's query parameters for every URL generated.
But, if this is a short lived demo that you can remove this custom code in a short time in the future, you can try this.
If it is code that would live forever I would not use it, as a mere update to the framework can break your project.