Add PropValueResource for managing property values; update TenantResource to include collapsed props data

This commit is contained in:
2026-06-22 16:19:24 -03:00
parent 55694b2048
commit 3bb8fbfd2f
2 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Domains\Prop\Resources;
use App\Domains\Prop\Concerns\Propable;
use App\Domains\Prop\Models\ModelPropValue;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use InvalidArgumentException;
/**
* @mixin ModelPropValue
*/
class PropValueResource extends JsonResource
{
/**
* @param iterable<ModelPropValue> $propValues
* @return array<string, mixed>
*/
public static function collapsed(iterable $propValues): array
{
return collect($propValues)
->mapWithKeys(fn (ModelPropValue $propValue) => [
$propValue->prop->codigo => $propValue->value,
])
->all();
}
/**
* @return array<string, mixed>
*/
public static function collapsedFromPropable(object $model): array
{
if (! in_array(Propable::class, class_uses_recursive($model), true)) {
throw new InvalidArgumentException('The given model must use the Propable trait.');
}
return static::collapsed(
$model->propValues()->with('prop')->get(),
);
}
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [$this->prop->codigo => $this->value];
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Domains\Tenant\Resources; namespace App\Domains\Tenant\Resources;
use App\Domains\Prop\Resources\PropValueResource;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
@@ -20,6 +21,7 @@ class TenantResource extends JsonResource
'codigo' => $this->codigo, 'codigo' => $this->codigo,
'nombre' => $this->nombre, 'nombre' => $this->nombre,
'dominio' => $this->dominio, 'dominio' => $this->dominio,
'props' => PropValueResource::collapsedFromPropable($this->resource),
'created_at' => $this->created_at, 'created_at' => $this->created_at,
'updated_at' => $this->updated_at, 'updated_at' => $this->updated_at,
]; ];