Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement attributeGroup #16

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions ShopFlow/app/Filament/Resources/AttributeGroupResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Filament\Resources\AttributeGroupResource\Pages;
use App\Models\AttributeGroup;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class AttributeGroupResource extends Resource
{
protected static ?string $model = AttributeGroup::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('ancestor_id')
->relationship('ancestor', 'name')
->required(),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('label')
->maxLength(255),
Forms\Components\TextInput::make('order')
->numeric(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('ancestor.name')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('label')
->searchable(),
Tables\Columns\TextColumn::make('order')
->numeric()
->sortable(),
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\ListAttributeGroups::route('/'),
'create' => Pages\CreateAttributeGroup::route('/create'),
'edit' => Pages\EditAttributeGroup::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\AttributeGroupResource\Pages;

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

class CreateAttributeGroup extends CreateRecord
{
protected static string $resource = AttributeGroupResource::class;

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

declare(strict_types=1);

namespace App\Filament\Resources\AttributeGroupResource\Pages;

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

class EditAttributeGroup extends EditRecord
{
protected static string $resource = AttributeGroupResource::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\AttributeGroupResource\Pages;

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

class ListAttributeGroups extends ListRecords
{
protected static string $resource = AttributeGroupResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
8 changes: 8 additions & 0 deletions ShopFlow/app/Models/Ancestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* App\Models\Ancestor
*
* @property positive-int $id
* @property string $name
* @property int|null $order
* @property Collection<AttributeGroup> $attributeGroups
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*/
Expand All @@ -25,4 +28,9 @@ class Ancestor extends Model
'name',
'order',
];

public function attributeGroups(): HasMany
{
return $this->hasMany(AttributeGroup::class);
}
}
40 changes: 40 additions & 0 deletions ShopFlow/app/Models/AttributeGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\AttributeGroup
*
* @property positive-int $id
* @property positive-int $ancestor_id
* @property string $name
* @property Ancestor $ancestor
* @property string|null $label
* @property int|null $order
*/
class AttributeGroup extends Model
{
use HasFactory;

protected $fillable = [
'ancestor_id',
'name',
'label',
'order',
];

protected $casts = [
'is_selective' => 'boolean',
];

public function ancestor(): BelongsTo
{
return $this->belongsTo(Ancestor::class);
}
}
29 changes: 29 additions & 0 deletions ShopFlow/database/factories/AttributeGroupFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Ancestor;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\AttributeGroup>
*/
class AttributeGroupFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'ancestor_id' => Ancestor::factory(),
'name' => $this->faker->word(),
'label' => $this->faker->word(),
'order' => $this->faker->numberBetween(1, 100),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

use App\Models\Ancestor;
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('attribute_groups', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Ancestor::class);
$table->string('name');
$table->string('label')->nullable();
$table->integer('order')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('attribute_groups');
}
};
48 changes: 48 additions & 0 deletions ShopFlow/database/seeders/AttributeGroupSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\AttributeGroup;
use Illuminate\Database\Seeder;

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

foreach ($data as $item) {
AttributeGroup::query()->updateOrCreate(
['name' => $item['name'], 'ancestor_id' => $item['ancestor_id']],
$item
);
}
}
}
4 changes: 2 additions & 2 deletions ShopFlow/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Database\Seeders;

use App\Models\Category;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
Expand All @@ -18,8 +17,9 @@ public function run(): void
RolePermissionSeeder::class,
AdminSeeder::class,
CitySeeder::class,
Category::class,
CategorySeeder::class,
AncestorSeeder::class,
AttributeGroupSeeder::class,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Filament\Resources\AncestorResource;
use App\Models\Ancestor;
use Filament\Actions\DeleteAction;
use Illuminate\Http\UploadedFile;

use function Pest\Laravel\get;
use function Pest\Livewire\livewire;
Expand Down Expand Up @@ -61,14 +60,12 @@
it('can create ancestor model.', function () {
// Arrange
$newAncestor = Ancestor::factory()->make();
$file = UploadedFile::fake()->image('avatar.png', 500);

livewire(AncestorResource\Pages\CreateAncestor::class)
->fillForm([
'name' => $newAncestor->name,
'order' => $newAncestor->order,
])
->set('data.image.path', [$file->getClientOriginalPath()])
->call('create')
->assertHasNoFormErrors();

Expand Down
Loading
Loading