Skip to content

Commit

Permalink
Fix small performance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed Nov 24, 2023
1 parent d3b3cf9 commit 533ad02
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/Http/Resources/AttributeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function toArray($request)
/** @var Attribute $attribute */
$attribute = $this->resource;

return $this->entity([
return $this->onEntity([
'name' => $attribute->name,
'value' => $attribute->value,
'parsed' => $attribute->mappedValue(),
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Resources/EntityChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Http\Resources;

use App\Models\Attribute;
use App\Models\MiscModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\Json\JsonResource;

class EntityChild extends JsonResource
Expand Down Expand Up @@ -34,4 +36,30 @@ public function entity(array $prepared = [])
ksort($final);
return $final;
}

/**
* Transform the resource into an array.
*
* @return array
*/
public function onEntity(array $prepared = [])
{
/** @var Model|Attribute|mixed $model */
$model = $this->resource;
$merged = [
'id' => $model->id,

'is_private' => (bool) $model->is_private,
'entity_id' => $model->entity_id,

'created_at' => $model->created_at,
'created_by' => $model->created_by,
'updated_at' => $model->updated_at,
'updated_by' => $model->updated_by,
];

$final = array_merge($prepared, $merged);
ksort($final);
return $final;
}
}
2 changes: 1 addition & 1 deletion app/Http/Resources/EntityEventResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function toArray($request)
{
/** @var EntityEvent $model */
$model = $this->resource;
return $this->entity([
return $this->onEntity([
'calendar_id' => $model->calendar_id,
'date' => $model->date(),
'day' => $model->day,
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Scopes/SubEntityScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public function scopeWithApi(Builder $query): Builder
'entity',
'entity.tags', 'entity.posts', 'entity.events',
'entity.relationships', 'entity.attributes', 'entity.inventories',
'entity.assets'
'entity.assets',
'entity.abilities',
];

$with = !empty($this->apiWith) ? $this->apiWith : [];
Expand Down

0 comments on commit 533ad02

Please sign in to comment.