调整数据字典搜索
parent
cf4fb3ff80
commit
17c7f0f9f8
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Filters;
|
||||||
|
|
||||||
|
use EloquentFilter\ModelFilter;
|
||||||
|
|
||||||
|
class KeywordFilter extends ModelFilter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 所属种植计划
|
||||||
|
*/
|
||||||
|
public function name($name)
|
||||||
|
{
|
||||||
|
return $this->where('name','like', '%'.$name.'%')
|
||||||
|
->orWhere('key','like', '%'.$name.'%');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Database\Events\QueryExecuted;
|
||||||
|
use Illuminate\Database\Events\TransactionBeginning;
|
||||||
|
use Illuminate\Database\Events\TransactionCommitted;
|
||||||
|
use Illuminate\Database\Events\TransactionRolledBack;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
|
class QueryLoggerServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function boot()
|
||||||
|
{
|
||||||
|
if (! config('app.debug')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->app['events']->listen([
|
||||||
|
QueryExecuted::class,
|
||||||
|
TransactionBeginning::class,
|
||||||
|
TransactionCommitted::class,
|
||||||
|
TransactionRolledBack::class,
|
||||||
|
], function ($event) {
|
||||||
|
Log::debug(match (true) {
|
||||||
|
$event instanceof TransactionBeginning => 'begin transaction',
|
||||||
|
$event instanceof TransactionCommitted => 'commit transaction',
|
||||||
|
$event instanceof TransactionRolledBack => 'rollback transaction',
|
||||||
|
default => $this->prepareSql($event),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Illuminate\Database\Events\QueryExecuted $query
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function prepareSql(QueryExecuted $query): string
|
||||||
|
{
|
||||||
|
$sql = str_replace(['%', '?'], ['%%', '%s'], $query->sql);
|
||||||
|
|
||||||
|
$bindings = $query->connection->prepareBindings($query->bindings);
|
||||||
|
|
||||||
|
if (count($bindings)) {
|
||||||
|
$sql = vsprintf($sql, array_map([$query->connection->getPdo(), 'quote'], $bindings));
|
||||||
|
}
|
||||||
|
|
||||||
|
return sprintf('[%s] %s', $this->formatDuration($query->time), $sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param float $milliseconds
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function formatDuration($milliseconds): string
|
||||||
|
{
|
||||||
|
return match (true) {
|
||||||
|
$milliseconds >= 1000 => round($milliseconds / 1000, 2).'s',
|
||||||
|
$milliseconds < 0.01 => round($milliseconds * 1000).'μs',
|
||||||
|
default => $milliseconds.'ms',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,8 +16,9 @@ class BaseService extends AdminService
|
||||||
|
|
||||||
public function getTree()
|
public function getTree()
|
||||||
{
|
{
|
||||||
$list = $this->query()->orderByDesc('sort')->get()->toArray();
|
$list = $this->query()->orderByDesc('sort')->get();
|
||||||
return array2tree($list);
|
$minNum = $list->min('parent_id');
|
||||||
|
return array2tree($list->toArray(), $minNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getModelFilter()
|
public function getModelFilter()
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace App\Services\Admin;
|
||||||
|
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use App\Models\Keyword;
|
use App\Models\Keyword;
|
||||||
|
use App\Models\Filters\KeywordFilter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @method Keyword getModel()
|
* @method Keyword getModel()
|
||||||
|
|
@ -12,6 +13,14 @@ use App\Models\Keyword;
|
||||||
class KeywordService extends BaseService
|
class KeywordService extends BaseService
|
||||||
{
|
{
|
||||||
protected string $modelName = Keyword::class;
|
protected string $modelName = Keyword::class;
|
||||||
|
protected string $modelFilterName = KeywordFilter::class;
|
||||||
|
|
||||||
|
public function getTree()
|
||||||
|
{
|
||||||
|
$list = $this->query()->filter(request()->all(), $this->modelFilterName)->orderByDesc('sort')->get();
|
||||||
|
$minNum = $list->min('parent_id');
|
||||||
|
return array2tree($list->toArray(), $minNum);
|
||||||
|
}
|
||||||
|
|
||||||
public function parentIsChild($id, $pid): bool
|
public function parentIsChild($id, $pid): bool
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -168,6 +168,8 @@ return [
|
||||||
// App\Providers\BroadcastServiceProvider::class,
|
// App\Providers\BroadcastServiceProvider::class,
|
||||||
App\Providers\EventServiceProvider::class,
|
App\Providers\EventServiceProvider::class,
|
||||||
App\Providers\RouteServiceProvider::class,
|
App\Providers\RouteServiceProvider::class,
|
||||||
|
|
||||||
|
App\Providers\QueryLoggerServiceProvider::class,
|
||||||
])->toArray(),
|
])->toArray(),
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue