few fixes and updates

master
Константин 2023-11-15 23:49:08 +03:00
parent 0137d31e46
commit c478fa1636
4 changed files with 5 additions and 22 deletions

View File

@ -24,24 +24,6 @@ class ObjectsController extends Controller {
$query = $this->model->query(); $query = $this->model->query();
$service = FiltersService::getService('objects'); $service = FiltersService::getService('objects');
$service->applyFilters($query, $filters); $service->applyFilters($query, $filters);
/*
if (($val = $request->get('type')) && ($type = ObjectType::query()->where(['name' => $val])->first())) {
$query->whereHas('type', function ($query) use ($val) {
$query->where('name', $val)->orWhereHas('parent', function ($query) use ($val) {
$query->where('name', $val);
});
});
if (in_array('product', [$type->name, $type->parent->name ?? null]) && ($field = Field::byUuidOrName('specification-holder')->first())) {
$field->applyFilter($query, $request->user()->companies->pluck('uuid')->all());
}
}
$filters = collect($request->has('filters') ? json_decode($request->get('filters'), true) : []);
$filters->each(function($value, $prop) use($query) {
if ($prop === 'search') $this->model->applySearchFilter($query, $value);
elseif ($field = Field::byUuidOrName($prop)->first()) $field->applyFilter($query, $value);
});
$query->orderBy('created_at', 'desc');
*/
$paginator = $query->paginate(config('app.pagination_limit')); $paginator = $query->paginate(config('app.pagination_limit'));
return fractal($paginator, new ObjectTransformer())->respond(); return fractal($paginator, new ObjectTransformer())->respond();
} }

View File

@ -54,7 +54,7 @@ class NirObject extends Model {
} }
public function objects(): MorphToMany { public function objects(): MorphToMany {
return $this->morphToMany(NirObject::class, 'objectable'); return $this->morphToMany(NirObject::class, 'objectable')->withPivot(['ord', 'group']);
} }
public function objectables($related): MorphToMany { public function objectables($related): MorphToMany {

View File

@ -38,9 +38,10 @@ trait HasObjectsTrait {
} }
public function attachObject(NirObject $object, $ord = null, $group = null) { public function attachObject(NirObject $object, $ord = null, $group = null) {
$group = $group ?? 'default';
$ord = ($ord === null) ? $this->getMaxObjectOrd($group) : $ord; $ord = ($ord === null) ? $this->getMaxObjectOrd($group) : $ord;
$this->moveObjectsSet('forward', $ord, null, $group); $this->moveObjectsSet('forward', $ord, null, $group);
$this->objects()->attach($object->id, ['ord' => $ord ?? 0, 'group' => $group ?? 'default']); $this->objects()->attach($object->id, ['ord' => $ord ?? 0, 'group' => $group]);
} }
public function getMaxObjectOrd($group = null): int { public function getMaxObjectOrd($group = null): int {
@ -71,7 +72,7 @@ trait HasObjectsTrait {
public function trimIndexes($groups) { public function trimIndexes($groups) {
collect(is_array($groups) ? $groups : [$groups])->unique()->each(function($group) { collect(is_array($groups) ? $groups : [$groups])->unique()->each(function($group) {
$this->objectsByGroup($group)->each(function($object, $index) { $this->objectsByGroup($group)->orderByPivot('ord')->each(function($object, $index) {
if ($object->pivot->ord !== $index) $this->objects()->updateExistingPivot($object, ['ord' => $index]); if ($object->pivot->ord !== $index) $this->objects()->updateExistingPivot($object, ['ord' => $index]);
}); });
}); });

View File

@ -48,7 +48,7 @@ class ObjectTransformer extends TransformerAbstract {
} }
public function includeObjects(NirObject $model): Collection { public function includeObjects(NirObject $model): Collection {
return $this->collection($model->objects, new ObjectTransformer()); return $this->collection($model->objects()->orderByPivot('ord')->get(), new ObjectTransformer());
} }
public function includePermissions(NirObject $model): Primitive { public function includePermissions(NirObject $model): Primitive {