Skip to content

Commit

Permalink
create CategorySeeder.php
Browse files Browse the repository at this point in the history
Signed-off-by: Bahman026 <Bahman026@gmail.com>
  • Loading branch information
bahman026 committed Dec 21, 2024
1 parent 1d033a3 commit c957d96
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ShopFlow/app/Filament/Resources/CategoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static function table(Table $table): Table
->limit(60)
->searchable(),
Tables\Columns\TextColumn::make('status')
->getStateUsing(fn (Category $record): string => CategoryStatusEnum::from((int) $record->status)->label())
->color(fn (Category $record): string => CategoryStatusEnum::from((int) $record->status)->color())
->getStateUsing(fn (Category $record) => $record->status->label())
->color(fn (Category $record): string => $record->status->color())
->sortable(),
Tables\Columns\IconColumn::make('no_index')
->boolean(),
Expand Down
4 changes: 2 additions & 2 deletions ShopFlow/app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public static function table(Table $table): Table
->sortable(),
Tables\Columns\TextColumn::make('national_id')
->searchable(), Tables\Columns\TextColumn::make('status')
->getStateUsing(fn (User $user) => UserStatusEnum::from($user->status)->label())
->color(fn (User $user) => UserStatusEnum::from($user->status)->color())
->getStateUsing(fn (User $user) => $user->status->label())
->color(fn (User $user) => $user->status->color())
->badge(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
Expand Down
8 changes: 7 additions & 1 deletion ShopFlow/app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use App\Enums\CategoryStatusEnum;
use App\Enums\UserStatusEnum;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -23,7 +24,7 @@
* @property string|null $canonical
* @property positive-int|null $parent_id
* @property positive-int|null $image_id
* @property bool $status
* @property CategoryStatusEnum $status
* @property Collection<Image> $images
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
Expand All @@ -46,6 +47,11 @@ class Category extends Model
'status',
];

protected $casts = [
'no_index' => 'boolean',
'status' => CategoryStatusEnum::class,
];

public function images(): MorphMany
{
return $this->morphMany(Image::class, 'imageable');
Expand Down
4 changes: 3 additions & 1 deletion ShopFlow/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use App\Enums\RolesEnum;
use App\Enums\UserStatusEnum;
use Carbon\Carbon;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasName;
Expand All @@ -24,7 +25,7 @@
* @property Carbon $mobile_verified_at
* @property string $national_id
* @property string $login_token
* @property positive-int $status
* @property UserStatusEnum $status
* @property Collection<Address> $addresses
* @property Carbon|null $email_verified_at
* @property Carbon|null $login_token_expire_time
Expand Down Expand Up @@ -74,6 +75,7 @@ protected function casts(): array
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'status' => UserStatusEnum::class,
];
}

Expand Down
115 changes: 114 additions & 1 deletion ShopFlow/database/seeders/CategorySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Database\Seeders;

use App\Enums\CategoryStatusEnum;
use App\Models\Category;
use Illuminate\Database\Seeder;
use Illuminate\Support\Arr; // Import the Arr facade

class CategorySeeder extends Seeder
{
Expand All @@ -13,6 +16,116 @@ class CategorySeeder extends Seeder
*/
public function run(): void
{
//
$categories = [
[
'id' => 1,
'heading' => 'لوازم جانبی گوشی',
'slug' => 'mobile-accessories',
'content' => 'انواع لوازم جانبی گوشی موبایل.',
'title' => 'لوازم جانبی گوشی',
'description' => 'لوازم جانبی متنوع برای گوشی‌های موبایل.',
'no_index' => false,
'canonical' => null,
'parent_id' => null,
'status' => CategoryStatusEnum::ACTIVE,
'children' => [
[
'id' => 2,
'heading' => 'قاب و کاور گوشی',
'slug' => 'phone-cases',
'content' => 'محافظ‌های متنوع برای گوشی‌های موبایل.',
'title' => 'قاب و کاور گوشی',
'description' => 'انواع قاب و کاور با طراحی‌های زیبا.',
'no_index' => false,
'canonical' => null,
'parent_id' => 1,
'status' => CategoryStatusEnum::ACTIVE,
],
[
'id' => 3,
'heading' => 'محافظ صفحه نمایش',
'slug' => 'screen-protectors',
'content' => 'محافظ صفحه‌های مختلف برای گوشی.',
'title' => 'محافظ صفحه نمایش',
'description' => 'محافظت از صفحه نمایش گوشی شما.',
'no_index' => false,
'canonical' => null,
'parent_id' => 1,
'status' => CategoryStatusEnum::ACTIVE,
],
[
'id' => 4,
'heading' => 'پاوربانک',
'slug' => 'power-banks',
'content' => 'انواع پاوربانک با ظرفیت‌های مختلف.',
'title' => 'پاوربانک',
'description' => 'پاوربانک‌های باکیفیت برای شارژ دستگاه‌ها.',
'no_index' => false,
'canonical' => null,
'parent_id' => 1,
'status' => CategoryStatusEnum::ACTIVE,
],
],
],
[
'id' => 5,
'heading' => 'لوازم خانگی',
'slug' => 'home-appliances',
'content' => 'محصولات برقی و لوازم خانگی با کیفیت.',
'title' => 'لوازم خانگی',
'description' => 'لوازم خانگی متنوع و مدرن.',
'no_index' => false,
'canonical' => null,
'parent_id' => null,
'status' => CategoryStatusEnum::ACTIVE,
'children' => [
[
'id' => 6,
'heading' => 'یخچال و فریزر',
'slug' => 'refrigerators',
'content' => 'انواع یخچال و فریزر با تکنولوژی روز.',
'title' => 'یخچال و فریزر',
'description' => 'لوازم ضروری برای نگهداری مواد غذایی.',
'no_index' => false,
'canonical' => null,
'parent_id' => 5,
'status' => CategoryStatusEnum::ACTIVE,
],
],
],
];

foreach ($categories as $category) {
// Remove the 'children' key before inserting
$categoryData = Arr::except($category, 'children');

Category::query()->updateOrCreate(
['id' => $category['id']],
$categoryData
);

if (isset($category['children'])) {
$this->createChildren($category['children'], $category['id']);
}
}
}

private function createChildren(array $children, int $parentId): void
{
foreach ($children as $child) {
$child['parent_id'] = $parentId;

// Remove the 'children' key before inserting
$childData = Arr::except($child, 'children');

Category::query()->updateOrCreate(
['id' => $child['id']],
$childData
);

if (isset($child['children'])) {
$this->createChildren($child['children'], $child['id']);
}
}
}
}

0 comments on commit c957d96

Please sign in to comment.