PHP code example of kyrax324 / laravel-queriplex

1. Go to this page and download the library: Download kyrax324/laravel-queriplex library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kyrax324 / laravel-queriplex example snippets



public $sortingKey = "sortBy"; // #1

public function filterRules()
{
	return [
		// with callback function
		'country' => function($query, $value){
			$query->where("country_code", $value);
		},
		// with shortcut alias
		'country' => 'country_code', // when isset 'country', where "country_code" = $value
	];
}

public function sortRules()
{
	return [
		"alphabet_asc" => fn ($query) => $query->orderBy('name', "ASC"),
		"alphabet_desc" => fn ($query) => $query->orderBy('name', "DESC"),
		"latest" => fn ($query) => $query->orderBy('created_at', "DESC"),
	];
}

use App\Queriplex\UserQueriplex;

...

// payload from request->validated()
$payload = [
	"country" => "ABC", // similar to when(isset(payload['country']), $callback)
	"sortBy" => "latest", // sortingKey #1
];

$query = User::query();
$query = UserQueriplex::make($query, $payload);

$result = $query->get();

/**
 * As result,
 * filter rule "country_code" & sort rule "latest" will be applied
 *
 * SQL Statement:
 * SELECT * from `users`
 * WHERE `country_code` = "ABC"
 * ORDER BY `created_at` DESC
 */

sh
php artisan make:queriplex UserQueriplex
sh
php artisan queriplex:publish-stub