-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from bahman026/implement_attribute
Implement attribute
- Loading branch information
Showing
12 changed files
with
412 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
ShopFlow/app/Filament/Resources/AttributeResource/Pages/CreateAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
ShopFlow/app/Filament/Resources/AttributeResource/Pages/EditAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ShopFlow/app/Filament/Resources/AttributeResource/Pages/ListAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
ShopFlow/database/migrations/2024_12_24_145428_create_attributes_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ] | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.