PHP Classes

File: wiki/Eloquent-Criteria-Parser.md

Recommend this page to a friend!
  Classes of Unay Santisteban   On Laravel Eloquent Query Builder   wiki/Eloquent-Criteria-Parser.md   Download  
File: wiki/Eloquent-Criteria-Parser.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: On Laravel Eloquent Query Builder
Compose criteria queries using Laravel Eloquent
Author: By
Last change:
Date: 6 months ago
Size: 1,661 bytes
 

Contents

Class file image Download

Eloquent Criteria Parser

Using the Eloquent Criteria Parser is quite simple. Just instantiate the class, next, pass a Criteria instance to the applyCriteria() method along with a EloquentQueryBuilder instance:

$parser = new EloquentCriteriaParser();
$query = $parser->applyCriteria(User::query(), $criteria);

The returned EloquentQueryBuilder has the criteria applied. You just need to call the get method to fetch the data from the database.

$users = $query->get();

Alternatively, you can pass an array of strings to map the attributes between the domain and the database.

$parser = new EloquentCriteriaParser([
    'domain-attribute' => 'database-attribute',
]);

For example, given the following table:

$this->builder->create('users', function (Blueprint $table) {
    $table->uuid('id');
    $table->string('first_name');
    $table->string('last_name');
    $table->string('email')->unique();
    $table->string('bio')->nullable();
    $table->timestamps();
});

You may use the following configuration to use name and surname instead of first_name and last_name:

$parser = new EloquentCriteriaParser([
    'name' => 'first_name',
    'surname' => 'last_name'
]);

A criteria search will be something like this:

$criteria = Criteria::default()
    ->withFilterGroup(FilterGroup::create()
        ->addFilterEqual('name', 'Vicent'));

$builder = User::query();

$parser = new EloquentCriteriaParser();
$users = $parser
    ->applyCriteria($builder, $criteria)
    ->get();

This is useful to expose different attributes from different interfaces as HTTP, or CLI.