ldc-bogt/app/Http/Controllers/Controller.php
Jérémy Munsch 7cb304e967
Full traça poulets
Cafel APi Eat
Xml flux new
Full Models Rework
Refactorize
2022-06-08 11:44:01 +02:00

77 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Session;
class Controller extends BaseController
{
use AuthorizesRequests;
use DispatchesJobs;
use ValidatesRequests;
/**
* Controller
*/
public function __construct()
{
$this->middleware(function ($request, $next) {
// * Flush poultry search if timed out
/** @var \Illuminate\Support\Carbon|null $time */
if ($time = session()->get('poultrySearch.time')) {
if ($time->lessThan(now()->subMinutes(30))) {
session()->forget('poultrySearch');
} else {
// * Refresh search session
Session::put('poultrySearch.time', now());
}
} else {
session()->forget('poultrySearch');
}
return $next($request);
});
}
/**
* Handle search for model.index
*
* @param Builder $query The eloquent query builder.
* @param string $table The query table string.
* @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 $table,
string $search,
...$field
): Builder {
if (count($field)) {
$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;
}
}