Skip to content

Commit

Permalink
Merge pull request #12 from bahman026/implement_category
Browse files Browse the repository at this point in the history
create CategorySeeder.php
  • Loading branch information
bahman026 authored Dec 21, 2024
2 parents 1d033a3 + d00b75a commit 7fbb3f2
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 12 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
11 changes: 4 additions & 7 deletions ShopFlow/app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;

Expand All @@ -41,7 +42,7 @@ public static function form(Form $form): Form
->required()
->options(UserStatusEnum::options())
->default(UserStatusEnum::ACTIVE->value)
->dehydrateStateUsing(fn (string $state): int => (int) $state),
->dehydrateStateUsing(fn (UserStatusEnum $state): UserStatusEnum => $state),

Forms\Components\TextInput::make('email')
->email()
Expand Down Expand Up @@ -149,11 +150,7 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('mobile_verified_at')
->dateTime()
->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())
->badge(),
Tables\Columns\TextColumn::make('national_id'),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
Expand Down Expand Up @@ -196,7 +193,7 @@ public static function getEloquentQuery(): Builder

public static function canCreate(): bool
{
return false;
return Auth::user()->hasRole('super-admin');
}

public static function getPages(): array
Expand Down
7 changes: 6 additions & 1 deletion ShopFlow/app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 +46,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 7fbb3f2

Please sign in to comment.