Query Parameter Utility

The QueryParameterUtil in the LaraUtilX package provides functionality for parsing query parameters from a request based on a list of allowed parameters.

Method

  1. parse(Request $request, array $allowedParameters): array: Parses query parameters from a request.

Usage

  1. Parse Query Parameters: To parse query parameters from a request, use the parse method:

    use 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'];
       }
    }

Result

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.

Publish

You can no longer publish this class, and you do not need to. A published copy kept its LaraUtilX namespace while landing in app/, where Composer's PSR-4 mapping expects App\, so the copy was never autoloaded and every call still resolved to the package.

Extend or wrap it instead:

namespace App\Support;

use LaraUtilX\Utilities\QueryParameterUtil;

class AppQueryParameters extends QueryParameterUtil
{
    // override what you need
}

Then bind your subclass in a service provider if you want it resolved in place of the package's:

$this->app->bind(\LaraUtilX\Utilities\QueryParameterUtil::class, \App\Support\AppQueryParameters::class);



This utility simplifies the process of extracting and handling query parameters from HTTP requests, ensuring that only allowed parameters are considered.