feat: introduce ProductCategoryResource and refactor database migrations by deleting old tables and consolidating transaction schema.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Api\Service;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Service\ProductCategoryResource;
|
||||
use App\Http\Resources\Api\Service\ServiceResource;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductCategories;
|
||||
@@ -63,7 +64,7 @@ class ProductCategoryController extends Controller
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Lấy dữ liệu thành công',
|
||||
'data' => $categories,
|
||||
'data' => ProductCategoryResource::collection($categories),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductCategoryResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'parent_id' => $this->parent_id,
|
||||
'is_active' => $this->is_active,
|
||||
'sort_order' => $this->sort_order,
|
||||
'type' => $this->type,
|
||||
'type_label' => $this->type_label,
|
||||
'children' => $this->when($this->children, function () {
|
||||
return self::collection($this->children);
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
+15
-1
@@ -32,7 +32,6 @@ class Product extends Model
|
||||
'is_unlimited',
|
||||
'is_active',
|
||||
'sort_order',
|
||||
'product_type',
|
||||
'product_categories_id',
|
||||
'published_at',
|
||||
];
|
||||
@@ -42,8 +41,23 @@ class Product extends Model
|
||||
'is_unlimited' => 'boolean',
|
||||
'published_at' => 'date',
|
||||
'gallery' => 'array',
|
||||
'product_type' => 'string',
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::saving(function ($product) {
|
||||
if ($product->product_categories_id) {
|
||||
$category = ProductCategories::find($product->product_categories_id);
|
||||
if ($category && $category->type) {
|
||||
$product->product_type = $category->type;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship: Product belongs to a Category
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\Product\ProductTypeEnums;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
@@ -18,6 +19,7 @@ class ProductCategories extends Model
|
||||
'parent_id',
|
||||
'is_active',
|
||||
'sort_order',
|
||||
'type',
|
||||
];
|
||||
|
||||
public function parent()
|
||||
@@ -34,4 +36,17 @@ class ProductCategories extends Model
|
||||
{
|
||||
return $this->hasMany(Product::class, 'product_categories_id');
|
||||
}
|
||||
|
||||
public function getTypeLabelAttribute(): ?string
|
||||
{
|
||||
if (!$this->type) {
|
||||
return null;
|
||||
}
|
||||
return ProductTypeEnums::tryFrom($this->type)?->label();
|
||||
}
|
||||
|
||||
public static function getTypeOptions(): array
|
||||
{
|
||||
return array_map(fn($case) => $case->value, ProductTypeEnums::cases());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user