PHP Classes

File: wiki/

Recommend this page to a friend!
  Classes of Unay Santisteban   On Laravel Eloquent Query Builder   wiki/   Download  
File: wiki/
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: 1 month ago
Size: 1,661 bytes


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) {

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()
        ->addFilterEqual('name', 'Vicent'));

$builder = User::query();

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

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