2020-07-23 10:29:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2021-06-14 17:47:50 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2020-07-23 10:29:36 +02:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
|
|
|
use Illuminate\Routing\Controller as BaseController;
|
2021-06-15 12:12:56 +02:00
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
|
use Illuminate\Support\Facades\Session;
|
2020-07-23 10:29:36 +02:00
|
|
|
|
|
|
|
|
class Controller extends BaseController
|
|
|
|
|
{
|
2021-03-20 14:38:15 +01:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
use DispatchesJobs;
|
|
|
|
|
use ValidatesRequests;
|
2021-06-14 17:47:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Undocumented function
|
|
|
|
|
*
|
|
|
|
|
* @param Builder $query The eloquent query builder
|
|
|
|
|
* @param string $search The query string
|
|
|
|
|
* @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)) {
|
2021-06-15 12:12:56 +02:00
|
|
|
$table = $query->getModel()->getTable();
|
|
|
|
|
$query = $query->where(function ($q) use ($field, $search, $table) {
|
2021-06-14 17:47:50 +02:00
|
|
|
foreach ($field as $f) {
|
|
|
|
|
$f = str_replace('?', '\?', $f);
|
|
|
|
|
if (is_array($f)) {
|
|
|
|
|
$q->orWhereRaw(
|
2021-06-15 12:12:56 +02:00
|
|
|
sprintf("UPPER(CONCAT(%s)) LIKE UPPER(?)", collect($f)->map(function ($q) use ($table) {
|
|
|
|
|
return "`$table`.`$q`";
|
2021-06-14 17:47:50 +02:00
|
|
|
})->implode(', \' \', ')),
|
|
|
|
|
["%$search%"]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2021-06-15 12:12:56 +02:00
|
|
|
$q->orWhereRaw("UPPER(`$table`.`$f`) LIKE UPPER(?)", ["%$search%"]);
|
2021-06-14 17:47:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
2021-06-15 12:12:56 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sort columns with a query
|
|
|
|
|
*
|
|
|
|
|
* @param Builder $query
|
|
|
|
|
* @param array $ignore
|
|
|
|
|
* @return Builder
|
|
|
|
|
*/
|
|
|
|
|
protected function sortQuery(Builder $query, array $ignore = []): Builder
|
|
|
|
|
{
|
|
|
|
|
$rName = request()->route()->getName();
|
|
|
|
|
$table = $query->getModel()->getTable();
|
|
|
|
|
if (request()->sort_col) {
|
|
|
|
|
Session::put("$rName.sort_col", request()->sort_col);
|
|
|
|
|
} else {
|
|
|
|
|
Session::remove("$rName.sort_col");
|
|
|
|
|
}
|
|
|
|
|
if (request()->sort_way) {
|
|
|
|
|
Session::put("$rName.sort_way", request()->sort_way);
|
|
|
|
|
} else {
|
|
|
|
|
Session::remove("$rName.sort_way");
|
|
|
|
|
}
|
|
|
|
|
if (request()->sort_col and request()->sort_way) {
|
|
|
|
|
if (
|
|
|
|
|
!isset($ignore) or
|
|
|
|
|
(isset($ignore) and !in_array(request()->sort_col, $ignore))
|
|
|
|
|
) {
|
|
|
|
|
$query = $query->orderBy(\sprintf("$table.%s", request()->sort_col), request()->sort_way);
|
|
|
|
|
}
|
|
|
|
|
} elseif (Schema::hasColumn($table, 'order')) {
|
|
|
|
|
$query = $query->orderBy('order', 'ASC');
|
|
|
|
|
Session::put("$rName.sort_col", 'order');
|
|
|
|
|
Session::put("$rName.sort_way", 'asc');
|
|
|
|
|
}
|
|
|
|
|
Session::save();
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
2020-07-23 10:29:36 +02:00
|
|
|
}
|