- add Civilité enum to Eleveur - add type enum to eleveur - add search bar on eleveurs - Rewrite eleveur CRUD - Add flashMessages module
52 lines
1.8 KiB
PHP
52 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
|
|
class Controller extends BaseController
|
|
{
|
|
use AuthorizesRequests;
|
|
use DispatchesJobs;
|
|
use ValidatesRequests;
|
|
|
|
/**
|
|
* Handle search for model.index
|
|
*
|
|
* @param Builder $query The eloquent query builder.
|
|
* @param string $search The query string.
|
|
* @phpcs:ignore Squiz.Commenting.FunctionComment.ParamCommentFullStop
|
|
* @param string|array ...$field Either a simple array or a string.
|
|
* In case of a string the two fields would be concatenate to make the search
|
|
* @return Builder
|
|
*/
|
|
protected function searchQuery(
|
|
Builder $query,
|
|
string $search,
|
|
...$field
|
|
): Builder {
|
|
if (count($field)) {
|
|
$table = $query->getModel()->getTable();
|
|
$query = $query->where(function ($q) use ($field, $search, $table) {
|
|
foreach ($field as $f) {
|
|
$f = str_replace('?', '\?', $f);
|
|
if (is_array($f)) {
|
|
$q->orWhereRaw(
|
|
sprintf("UPPER(CONCAT(%s)) LIKE UPPER(?)", collect($f)->map(function ($q) use ($table) {
|
|
return "`$table`.`$q`";
|
|
})->implode(', \' \', ')),
|
|
["%$search%"]
|
|
);
|
|
} else {
|
|
$q->orWhereRaw("UPPER(`$table`.`$f`) LIKE UPPER(?)", ["%$search%"]);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
return $query;
|
|
}
|
|
}
|