|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Flavorly\VanillaComponents\Core\Components\Helpers; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 6 | +use Illuminate\Database\Eloquent\Builder; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use Laravel\Scout\Searchable; |
| 10 | + |
| 11 | +class ResolveRichSelectOptions |
| 12 | +{ |
| 13 | + public function __construct( |
| 14 | + protected string $model, |
| 15 | + protected array $columns = [], |
| 16 | + protected bool $useScout = true, |
| 17 | + protected int $perPage = 10, |
| 18 | + ) |
| 19 | + {} |
| 20 | + |
| 21 | + /** |
| 22 | + * Builds the query and returns the paginator |
| 23 | + * |
| 24 | + * @return LengthAwarePaginator |
| 25 | + */ |
| 26 | + public function response(): LengthAwarePaginator |
| 27 | + { |
| 28 | + // Store the values |
| 29 | + $searchQuery = request()->query('query'); |
| 30 | + $values = request()->query('values'); |
| 31 | + $page = request()->query('page', 1); |
| 32 | + |
| 33 | + /** @var Model|Builder $model */ |
| 34 | + $model = app($this->model); |
| 35 | + $scoutKeys = []; // Stores Laravel scout temporary keys |
| 36 | + |
| 37 | + // If none is filled, return teh latest paginated results |
| 38 | + if(!filled($searchQuery) && !filled($values)){ |
| 39 | + return $model::query()->latest()->paginate($this->perPage); |
| 40 | + } |
| 41 | + |
| 42 | + if($this->useScout && in_array(Searchable::class, class_uses($model::class))){ |
| 43 | + /** @var Searchable $model */ |
| 44 | + $scoutKeys = $model::search($searchQuery ?? '')->keys() ?? []; |
| 45 | + } |
| 46 | + |
| 47 | + return $model::query() |
| 48 | + // If we have values, then we can instantly filter them here |
| 49 | + ->when(filled($values), function($query) use($model, $values) { |
| 50 | + $query->whereIn($model->getKeyName(), !is_array($values) ? Str::of($values)->explode(',') : $values); |
| 51 | + }) |
| 52 | + // Not using scout and using regular search |
| 53 | + ->when(!$this->useScout && filled($searchQuery) && !empty($this->columns), function($query) use($model, $searchQuery, $scoutKeys) { |
| 54 | + $query->where(function($query) use($searchQuery, $model, $scoutKeys) { |
| 55 | + foreach ($this->columns as $column) { |
| 56 | + $query->orWhere($column, 'like', "%{$searchQuery}%"); |
| 57 | + } |
| 58 | + }); |
| 59 | + }) |
| 60 | + // Using Scout |
| 61 | + ->when($this->useScout && filled($searchQuery) && !empty($scoutKeys), function($query) use($model, $scoutKeys) { |
| 62 | + $query->whereIn($model->getKeyName(), $scoutKeys); |
| 63 | + }) |
| 64 | + // Finally paginate the results |
| 65 | + ->paginate(perPage: $this->perPage, page: $page); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Resolves the current select options for a given model and given columns |
| 70 | + * Supporting scout searching |
| 71 | + * This function is intended to be used for frontend pooling with Rich Select |
| 72 | + * |
| 73 | + * @param string $model |
| 74 | + * @param array $columns |
| 75 | + * @param bool $useScout |
| 76 | + * @return LengthAwarePaginator |
| 77 | + * @throws \Exception |
| 78 | + */ |
| 79 | + public static function for(string $model, array $columns = [], bool $useScout = true): LengthAwarePaginator |
| 80 | + { |
| 81 | + return (new static($model, $columns, $useScout))->response(); |
| 82 | + } |
| 83 | +} |
0 commit comments