The QueryParameterUtil
in the LaraUtilX
package provides functionality for parsing query parameters from a request based on a list of allowed parameters.
parse(Request $request, array $allowedParameters): array
: Parses query parameters from a request.Parse Query Parameters:
To parse query parameters from a request, use the parse
method:
use omarchouman\LaraUtilX\Utilities\QueryParameterUtil;
use Illuminate\Http\Request;
class MyController extends Controller
{
public function index(Request $request)
{
$allowedParameters = ['param1', 'param2', 'param3']; // Define allowed parameters
$queryParams = QueryParameterUtil::parse($request, $allowedParameters);
// Use parsed query parameters in your logic
// For example:
// $param1Value = $queryParams['param1'];
}
}
The utility returns an array containing the parsed query parameters.
Success Result: The parsed query parameters are returned as an associative array, where the keys are the parameter names and the values are their corresponding values from the request.
Empty Result: If none of the allowed parameters are present in the request, an empty array is returned.
You can publish this utility through the below command:
php artisan vendor:publish --tag=lara-util-x-query-parameter
This utility simplifies the process of extracting and handling query parameters from HTTP requests, ensuring that only allowed parameters are considered.