Skip to content

Commit

Permalink
Merge pull request #18 from bahman026/implement_attribute
Browse files Browse the repository at this point in the history
Implement attribute
  • Loading branch information
bahman026 authored Dec 24, 2024
2 parents b9305df + 28c6c46 commit f699487
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 49 deletions.
82 changes: 82 additions & 0 deletions ShopFlow/app/Filament/Resources/AttributeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Filament\Resources\AttributeResource\Pages;
use App\Models\Attribute;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class AttributeResource extends Resource
{
protected static ?string $model = Attribute::class;

protected static ?string $navigationGroup = 'Attribute';

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('attribute_group_id')
->relationship('attributeGroup', 'name')
->required(),
Forms\Components\TextInput::make('value')
->required()
->maxLength(255),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('attributeGroup.name')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('value')
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
// Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListAttributes::route('/'),
'create' => Pages\CreateAttribute::route('/create'),
'edit' => Pages\EditAttribute::route('/{record}/edit'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\AttributeResource\Pages;

use App\Filament\Resources\AttributeResource;
use Filament\Resources\Pages\CreateRecord;

class CreateAttribute extends CreateRecord
{
protected static string $resource = AttributeResource::class;

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\AttributeResource\Pages;

use App\Filament\Resources\AttributeResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditAttribute extends EditRecord
{
protected static string $resource = AttributeResource::class;

protected function getHeaderActions(): array
{
return [
// Actions\DeleteAction::make(),
];
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\AttributeResource\Pages;

use App\Filament\Resources\AttributeResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListAttributes extends ListRecords
{
protected static string $resource = AttributeResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
32 changes: 32 additions & 0 deletions ShopFlow/app/Models/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* App\Models\Attribute
*
* @property positive-int $id
* @property positive-int $attribute_group_id
* @property string $value
* @property AttributeGroup $attributeGroup
*/
class Attribute extends Model
{
use HasFactory;

protected $fillable = [
'attribute_group_id',
'value',
];

public function attributeGroup(): BelongsTo
{
return $this->belongsTo(AttributeGroup::class);
}
}
6 changes: 6 additions & 0 deletions ShopFlow/app/Models/AttributeGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* App\Models\AttributeGroup
Expand Down Expand Up @@ -37,4 +38,9 @@ public function ancestor(): BelongsTo
{
return $this->belongsTo(Ancestor::class);
}

public function attributes(): HasMany
{
return $this->hasMany(Attribute::class);
}
}
25 changes: 25 additions & 0 deletions ShopFlow/database/factories/AttributeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Attribute;
use App\Models\AttributeGroup;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Attribute>
*/
class AttributeFactory extends Factory
{
protected $model = Attribute::class;

public function definition(): array
{
return [
'attribute_group_id' => AttributeGroup::factory(),
'value' => $this->faker->word,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

use App\Models\AttributeGroup;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('attributes', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(AttributeGroup::class);
$table->string('value');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('attributes');
}
};
48 changes: 0 additions & 48 deletions ShopFlow/database/seeders/AttributeGroupSeeder.php

This file was deleted.

81 changes: 81 additions & 0 deletions ShopFlow/database/seeders/AttributeSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\Attribute;
use App\Models\AttributeGroup;
// Replace with your model name
use Illuminate\Database\Seeder;

// Replace with your model name

class AttributeSeeder extends Seeder
{
public function run(): void
{
$attributeGroupsData = [
[
'ancestor_id' => 1,
'name' => 'رنگ',
'label' => 'انتخاب رنگ',
'order' => 1,
'attributes' => [
['value' => 'قرمز'],
['value' => 'آبی'],
],
],
[
'ancestor_id' => 1,
'name' => 'حافظه',
'label' => 'مقدار حافظه',
'order' => 2,
'attributes' => [
['value' => '8GB'],
['value' => '16GB'],
],
],
[
'ancestor_id' => 2,
'name' => 'ابعاد',
'label' => 'اندازه‌های محصول',
'order' => 1,
],
[
'ancestor_id' => 2,
'name' => 'وزن',
'label' => 'وزن محصول',
'order' => 2,
],
];

foreach ($attributeGroupsData as $groupData) {
// Handle the AttributeGroup
$attributeGroup = AttributeGroup::query()->updateOrCreate(
[
'name' => $groupData['name'], // Unique identifier for update or create
],
[
'ancestor_id' => $groupData['ancestor_id'],
'label' => $groupData['label'],
'order' => $groupData['order'],
]
);

if (! empty($groupData['attributes'])) {
foreach ($groupData['attributes'] as $attributeData) {
Attribute::query()->firstOrCreate(
[
'value' => $attributeData['value'],
'attribute_group_id' => $attributeGroup->id,
],
[
'value' => $attributeData['value'],
'attribute_group_id' => $attributeGroup->id, ]
);
}
}
}
}
}
Loading

0 comments on commit f699487

Please sign in to comment.