EscolaLMS/Core

View on GitHub
src/Repositories/Criteria/UserSearchCriterion.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace EscolaLms\Core\Repositories\Criteria;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class UserSearchCriterion extends Criterion
{
    public function __construct($value = null)
    {
        parent::__construct(null, $value);
    }

    public function apply(Builder $query): Builder
    {
        return $query->where(function ($q) {
            $like = DB::connection()->getPdo()->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'pgsql' ? 'ILIKE' : 'LIKE';

            $q->where('first_name', $like, "%$this->value%")
                ->orWhere('last_name', $like, "%$this->value%")
                ->orWhere('email', $like, "%$this->value%");
        });
    }
}