PHP code example of drewlabs / rest-query

1. Go to this page and download the library: Download drewlabs/rest-query 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/ */

    

drewlabs / rest-query example snippets


use Drewlabs\RestQuery\QueryBuilder;

// ...


// Building the query and using the query builder fluent API
$builder = QueryBuilder::new()
            ->eq('title', 'Lorem Ipsum')
            ->neq('id', 10)
            ->where(function (QueryBuilder $builder) {
                return $builder->in('tags', ['I', 'L', 'F'])
                    ->gt('likes', 120)
                    ->gte('groups', 10);
            });

// Compiling the query output to JSON string
$result = $builder->json();

$result = QueryBuilder::new()->eq('title', 'Lorem Ipsum')->getQuery(); // ['where' => [['title, '=', 'Lorem Ipsum']]]

$result = QueryBuilder::new()->neq('title', 'Lorem Ipsum')->getQuery(); // ['where' => [['title, '<>, 'Lorem Ipsum']]]

$result = QueryBuilder::new()->lt('title', 'Lorem Ipsum')->getQuery(); // ['where' => [['title, '<', 'Lorem Ipsum']]]
$result = QueryBuilder::new()->lte('title', 'Lorem Ipsum')->getQuery(); // ['where' => [['title, '<=', 'Lorem Ipsum']]]

$result = QueryBuilder::new()->gt('title', 'Lorem Ipsum')->getQuery(); // ['where' => [['title, '>', 'Lorem Ipsum']]]
$result = QueryBuilder::new()->gte('title', 'Lorem Ipsum')->getQuery(); // ['where' => [['title, '>=', 'Lorem Ipsum']]]

$result = QueryBuilder::new()->in('rates', [3, 3.5, 9])->getQuery(); // ['in' => [['title, [3, 3.5, 9]]]]

$result = QueryBuilder::new()->exists('comments')->getQuery(); // ['has' => [['comments']]]
$result = QueryBuilder::new()->exists('comments', new SubQuery('where', ['likes', '>', 1000]))->getQuery(); // ['has' => [['column' => 'comments', 'method' => ['params' => ['likes', '>', 1000], 'method' => 'where' ]]]]

$result = QueryBuilder::new()->sort('created_at', -1)->getQuery(); // ['sort' => ['by' => 'created_at', 'order' => 'DESC']]

$columns = QueryBuilder::new()->eq('title', 'Lorem Ipsum')->select('*', 'comments')->getColumns(); // ['*', 'comments']