Introduction

In some cases, we want to retrieve data based on some dynamic input. This allows for more flexibility in the data we retrieve and how we display it.

This can be done with the help of parameters, which are passed to the SQL query when it is executed.

Parameters

The parameters are values that are passed to the SQL query when it is executed, either through View components, API requests, or using the CLI. To know more information about how parameters are used in each case, please refer to their own documentation.

In the query, the parameters can be used with the param() function. This function retrieves the value specified by the user and inserts it safely into the query.

SELECT *
FROM companies
WHERE id = {param('company_id')}

Fallback

If there is the parameter is not found, the query will fail. To avoid this, you can set a fallback value in the param() function.

SELECT *
FROM companies
WHERE id = {param('company_id', 'Latitude')} -- If no company_id is found, it will use 'Latitude' as the value

You could for example use the fallback value to check if the parameter is set or not before applying the filter.

SELECT *
FROM companies
{#if param('company_id', false)} -- If the parameter is not set, it will not apply the filter
  WHERE id = {param('company_id')}
{/if}