feat: implement import and export functionality for product categories and products
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use App\Models\ProductCategories;
|
||||
use Illuminate\Support\Str;
|
||||
use Maatwebsite\Excel\Concerns\ToModel;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
|
||||
class ProductCategoriesImport implements ToModel, WithHeadingRow, WithValidation
|
||||
{
|
||||
public function model(array $row)
|
||||
{
|
||||
$parentId = null;
|
||||
|
||||
if (!empty($row['parent_name'])) {
|
||||
$parent = ProductCategories::where('name', $row['parent_name'])->first();
|
||||
$parentId = $parent?->id;
|
||||
}
|
||||
|
||||
if (!empty($row['parent_id'])) {
|
||||
$parentId = (int) $row['parent_id'];
|
||||
}
|
||||
|
||||
return new ProductCategories([
|
||||
'name' => $row['name'],
|
||||
'slug' => $row['slug'] ?? Str::slug($row['name']),
|
||||
'parent_id' => $parentId,
|
||||
'type' => $row['type'] ?? null,
|
||||
'is_active' => in_array(strtolower($row['is_active'] ?? ''), ['yes', '1', 'true']),
|
||||
'sort_order' => $row['sort_order'] ?? 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|max:255',
|
||||
'slug' => 'nullable|string|max:255',
|
||||
'parent_name' => 'nullable|string|max:255',
|
||||
'parent_id' => 'nullable|integer|exists:product_categories,id',
|
||||
'type' => 'nullable|string|max:50',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductCategories;
|
||||
use Illuminate\Support\Str;
|
||||
use Maatwebsite\Excel\Concerns\ToModel;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
|
||||
class ProductsImport implements ToModel, WithHeadingRow, WithValidation
|
||||
{
|
||||
public function model(array $row)
|
||||
{
|
||||
$categoryName = $row['category'] ?? null;
|
||||
$categoryId = null;
|
||||
|
||||
if ($categoryName) {
|
||||
$category = ProductCategories::where('name', $categoryName)->first();
|
||||
$categoryId = $category?->id;
|
||||
}
|
||||
|
||||
if (!empty($row['category_id'])) {
|
||||
$categoryId = (int) $row['category_id'];
|
||||
}
|
||||
|
||||
return new Product([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'name' => $row['name'],
|
||||
'slug' => $row['slug'] ?? Str::slug($row['name']),
|
||||
'product_categories_id' => $categoryId,
|
||||
'short_description' => $row['short_description'] ?? null,
|
||||
'description' => $row['description'] ?? null,
|
||||
'setup_fee' => $row['setup_fee'] ?? 0,
|
||||
'trial_days' => $row['trial_days'] ?? 0,
|
||||
'cpu' => $row['cpu'] ?? null,
|
||||
'ram' => $row['ram'] ?? null,
|
||||
'gpu' => $row['gpu'] ?? null,
|
||||
'disk' => $row['disk'] ?? null,
|
||||
'bandwidth' => $row['bandwidth'] ?? null,
|
||||
'stock' => $row['stock'] ?? 0,
|
||||
'is_unlimited' => in_array(strtolower($row['is_unlimited'] ?? ''), ['yes', '1', 'true']),
|
||||
'is_active' => in_array(strtolower($row['is_active'] ?? ''), ['yes', '1', 'true']),
|
||||
'sort_order' => $row['sort_order'] ?? 0,
|
||||
'published_at' => !empty($row['published_at']) ? \Carbon\Carbon::parse($row['published_at']) : null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|max:255',
|
||||
'slug' => 'nullable|string|max:255',
|
||||
'category' => 'nullable|string|max:255',
|
||||
'category_id' => 'nullable|integer|exists:product_categories,id',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user