Skip to content

Commit

Permalink
(clone $query) => $query->clone()
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Dec 1, 2023
1 parent c922366 commit 7831b90
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/Controllers/AdminPermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function autoGenerate()
{
$menus = Admin::adminMenuModel()::query()->get()->toArray();
$slugMap = Admin::adminPermissionModel()::query()->get(['id', 'slug'])->keyBy('id')->toArray();
$slugCache = [];
$slugCache = [];
$permissions = [];
foreach ($menus as $menu) {
$_httpPath =
Expand All @@ -160,13 +160,13 @@ public function autoGenerate()
$menuTitle = sprintf('%s(%s)', $menuTitle, $menu['id']);
}

if($_httpPath){
if ($_httpPath) {
$slug = Str::of(explode('?', $_httpPath)[0])->trim('/')->replace('/', '.')->replace('*', '')->value();
}else{
} else {
$slug = Str::uuid();
}

if(in_array($slug, $slugCache)){
if (in_array($slug, $slugCache)) {
$slug = $slug . '.' . $menu['id'];
}
$slugCache[] = $slug;
Expand Down Expand Up @@ -199,7 +199,7 @@ public function autoGenerate()

$_id = $item['id'];
while ($item['parent_id'] != 0) {
(clone $query)->insert([
$query->clone()->insert([
'permission_id' => $_id,
'menu_id' => $item['parent_id'],
]);
Expand Down
5 changes: 3 additions & 2 deletions src/Services/AdminPermissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public function checkRepeated($data, $id = 0)
{
$query = $this->query()->when($id, fn($query) => $query->where('id', '<>', $id));

amis_abort_if((clone $query)->where('name', $data['name'])
amis_abort_if($query->clone()->where('name', $data['name'])
->exists(), __('admin.admin_permission.name_already_exists'));
amis_abort_if((clone $query)->where('slug', $data['slug'])

amis_abort_if($query->clone()->where('slug', $data['slug'])
->exists(), __('admin.admin_permission.slug_already_exists'));
}

Expand Down
7 changes: 5 additions & 2 deletions src/Services/AdminRoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ public function checkRepeated($data, $id = 0)
{
$query = $this->query()->when($id, fn($query) => $query->where('id', '<>', $id));

amis_abort_if((clone $query)->where('name', $data['name'])
amis_abort_if($query->clone()
->where('name', $data['name'])
->exists(), __('admin.admin_role.name_already_exists'));
amis_abort_if((clone $query)->where('slug', $data['slug'])

amis_abort_if($query->clone()
->where('slug', $data['slug'])
->exists(), __('admin.admin_role.slug_already_exists'));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Services/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public function list()
{
$query = $this->listQuery();

$items = (clone $query)->paginate(request()->input('perPage', 20))->items();
$total = (clone $query)->count();
$items = $query->clone()->paginate(request()->input('perPage', 20))->items();
$total = $query->clone()->count();

return compact('items', 'total');
}
Expand Down
7 changes: 3 additions & 4 deletions src/Services/AdminUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ public function list()
{
$keyword = request()->keyword;

$query = $this
->query()
$query = $this->query()
->with('roles')
->select(['id', 'name', 'username', 'avatar', 'created_at'])
->when($keyword, function ($query) use ($keyword) {
Expand All @@ -111,8 +110,8 @@ public function list()

$this->sortable($query);

$items = (clone $query)->paginate(request()->input('perPage', 20))->items();
$total = (clone $query)->count();
$items = $query->clone()->paginate(request()->input('perPage', 20))->items();
$total = $query->clone()->count();

return compact('items', 'total');
}
Expand Down

0 comments on commit 7831b90

Please sign in to comment.