feat(category): add banner functionality to categories with related resource and migration tests
This commit is contained in:
@@ -15,6 +15,7 @@ use App\Domains\Commerce\Catalog\Resources\CatalogFeaturedGroupResource;
|
||||
use App\Domains\Commerce\Catalog\Resources\CatalogItemDetailResource;
|
||||
use App\Domains\Commerce\Catalog\Resources\CatalogSearchItemResource;
|
||||
use App\Domains\Commerce\Catalog\Resources\CatalogVariantOptionsResource;
|
||||
use App\Domains\Commerce\Catalog\Resources\CategoryResource;
|
||||
use App\Domains\Commerce\Catalog\Services\CatalogItemAllowanceService;
|
||||
use App\Domains\Commerce\Catalog\Services\CatalogService;
|
||||
use App\Domains\Commerce\Catalog\Services\FeaturedGroupService;
|
||||
@@ -78,11 +79,7 @@ class CatalogController extends Controller
|
||||
$allowances->attach($items->getCollection(), $this->userId($request));
|
||||
|
||||
return CatalogSearchItemResource::collection($items)->additional([
|
||||
'category' => [
|
||||
'id' => $category->id,
|
||||
'nombre' => $category->nombre,
|
||||
'categoria_id' => $category->categoria_id,
|
||||
],
|
||||
'category' => CategoryResource::make($category)->resolve($request),
|
||||
'layout' => $tenant->search_product_layout->value,
|
||||
'group_layout' => $tenant->search_group_layout->value,
|
||||
]);
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Domains\Commerce\Catalog\Models;
|
||||
|
||||
use App\Domains\Core\Auth\Models\User;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -16,6 +17,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'categoria_id',
|
||||
'nombre',
|
||||
'is_enabled',
|
||||
'banner_id',
|
||||
'banner_enabled',
|
||||
])]
|
||||
class Category extends Model
|
||||
{
|
||||
@@ -25,6 +28,7 @@ class Category extends Model
|
||||
|
||||
protected $attributes = [
|
||||
'is_enabled' => true,
|
||||
'banner_enabled' => false,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -35,6 +39,8 @@ class Category extends Model
|
||||
return [
|
||||
'categoria_id' => 'integer',
|
||||
'is_enabled' => 'boolean',
|
||||
'banner_id' => 'integer',
|
||||
'banner_enabled' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -59,6 +65,12 @@ class Category extends Model
|
||||
return $this->belongsTo(self::class, 'categoria_id');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Attachment, $this> */
|
||||
public function banner(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Attachment::class, 'banner_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<Category, $this>
|
||||
*/
|
||||
|
||||
24
app/Domains/Commerce/Catalog/Resources/CategoryResource.php
Normal file
24
app/Domains/Commerce/Catalog/Resources/CategoryResource.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Commerce\Catalog\Resources;
|
||||
|
||||
use App\Domains\Commerce\Catalog\Models\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin Category */
|
||||
class CategoryResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'nombre' => $this->nombre,
|
||||
'categoria_id' => $this->categoria_id,
|
||||
'banner_url' => $this->banner_enabled
|
||||
? $this->banner?->getTemporaryUrl(1440)
|
||||
: null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user