feat: Add Chicken entity with model, migration, Filament resource, and status enum.
This commit is contained in:
parent
0a23f4d0e3
commit
dbd235fcbe
45
app/Enums/ChickenStatus.php
Normal file
45
app/Enums/ChickenStatus.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
use App\Traits\Enum\WithComment;
|
||||||
|
use App\Traits\Enum\WithValue;
|
||||||
|
use Filament\Support\Contracts\HasColor;
|
||||||
|
use Filament\Support\Contracts\HasLabel;
|
||||||
|
|
||||||
|
enum ChickenStatus: int implements HasColor, HasLabel
|
||||||
|
{
|
||||||
|
use WithComment, WithValue;
|
||||||
|
|
||||||
|
case ACTIVE = 1;
|
||||||
|
case DEAD = 2;
|
||||||
|
case CULLED = 3;
|
||||||
|
case SOLD = 4;
|
||||||
|
|
||||||
|
public function getLabel(): ?string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::ACTIVE => 'Aktif',
|
||||||
|
self::DEAD => 'Mati',
|
||||||
|
self::CULLED => 'Dimusnahkan',
|
||||||
|
self::SOLD => 'Dijual',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getColor(): string|array|null
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::ACTIVE => 'success',
|
||||||
|
self::DEAD => 'danger',
|
||||||
|
self::CULLED => 'warning',
|
||||||
|
self::SOLD => 'primary',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function options(): array
|
||||||
|
{
|
||||||
|
return collect(self::cases())
|
||||||
|
->mapWithKeys(fn ($case) => [$case->value => $case->getLabel()])
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
164
app/Filament/Resources/Master/Chickens/ChickenResource.php
Normal file
164
app/Filament/Resources/Master/Chickens/ChickenResource.php
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Chickens;
|
||||||
|
|
||||||
|
use App\Enums\ChickenStatus;
|
||||||
|
use App\Filament\Actions\Cheerful\DeleteAction;
|
||||||
|
use App\Filament\Actions\Cheerful\EditAction;
|
||||||
|
use App\Filament\Actions\Cheerful\ForceDeleteAction;
|
||||||
|
use App\Filament\Actions\Cheerful\RestoreAction;
|
||||||
|
use App\Filament\Actions\DefaultBulkActions;
|
||||||
|
use App\Filament\Columns\TimestampColumns;
|
||||||
|
use App\Filament\Resources\Master\Chickens\Pages\ManageChickens;
|
||||||
|
use App\Models\Chicken;
|
||||||
|
use BackedEnum;
|
||||||
|
use Filament\Actions\BulkActionGroup;
|
||||||
|
use Filament\Forms\Components\DatePicker;
|
||||||
|
use Filament\Forms\Components\Select;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Support\Enums\Width;
|
||||||
|
use Filament\Support\Icons\Heroicon;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Filters\SelectFilter;
|
||||||
|
use Filament\Tables\Filters\TrashedFilter;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
class ChickenResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = Chicken::class;
|
||||||
|
|
||||||
|
protected static string|UnitEnum|null $navigationGroup = 'Master';
|
||||||
|
|
||||||
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::RectangleGroup;
|
||||||
|
|
||||||
|
protected static ?string $navigationLabel = 'Ayam';
|
||||||
|
|
||||||
|
protected static ?int $navigationSort = 2;
|
||||||
|
|
||||||
|
protected static ?string $recordTitleAttribute = 'tag_code';
|
||||||
|
|
||||||
|
protected static ?string $slug = 'master/chickens';
|
||||||
|
|
||||||
|
public static function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema
|
||||||
|
->components([
|
||||||
|
TextInput::make('tag_code')
|
||||||
|
->label('Kode Tag')
|
||||||
|
->placeholder('...')
|
||||||
|
->autocomplete(false)
|
||||||
|
->autofocus()
|
||||||
|
->required()
|
||||||
|
->maxLength(20)
|
||||||
|
->unique(ignoreRecord: true),
|
||||||
|
|
||||||
|
DatePicker::make('hatch_date')
|
||||||
|
->label('Tanggal Menetas')
|
||||||
|
->placeholder('Pilih Tanggal')
|
||||||
|
->native(false)
|
||||||
|
->displayFormat('l, d F Y'),
|
||||||
|
|
||||||
|
DatePicker::make('arrival_date')
|
||||||
|
->label('Tanggal Tiba')
|
||||||
|
->placeholder('Pilih Tanggal')
|
||||||
|
->required()
|
||||||
|
->native(false)
|
||||||
|
->displayFormat('l, d F Y'),
|
||||||
|
|
||||||
|
DatePicker::make('exit_date')
|
||||||
|
->label('Tanggal Keluar/Afkir')
|
||||||
|
->placeholder('Pilih Tanggal')
|
||||||
|
->native(false)
|
||||||
|
->displayFormat('l, d F Y'),
|
||||||
|
|
||||||
|
Select::make('status')
|
||||||
|
->label('Status')
|
||||||
|
->options(ChickenStatus::class)
|
||||||
|
->required()
|
||||||
|
->native(false)
|
||||||
|
->default(ChickenStatus::ACTIVE),
|
||||||
|
])
|
||||||
|
->columns(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
TextColumn::make('tag_code')
|
||||||
|
->label('Kode Tag')
|
||||||
|
->searchable()
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
TextColumn::make('hatch_date')
|
||||||
|
->label('Tanggal Menetas')
|
||||||
|
->date('l, d F Y')
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
TextColumn::make('arrival_date')
|
||||||
|
->label('Tanggal Tiba')
|
||||||
|
->date('l, d F Y')
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
TextColumn::make('exit_date')
|
||||||
|
->label('Tanggal Keluar')
|
||||||
|
->date('l, d F Y')
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
TextColumn::make('status')
|
||||||
|
->label('Status')
|
||||||
|
->badge()
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
...TimestampColumns::make(),
|
||||||
|
])
|
||||||
|
->filters([
|
||||||
|
SelectFilter::make('status')
|
||||||
|
->label('Status')
|
||||||
|
->options(ChickenStatus::class)
|
||||||
|
->native(false),
|
||||||
|
|
||||||
|
TrashedFilter::make()
|
||||||
|
->native(false),
|
||||||
|
])
|
||||||
|
->recordActions([
|
||||||
|
EditAction::make()
|
||||||
|
->modalWidth(Width::Large),
|
||||||
|
|
||||||
|
DeleteAction::make(),
|
||||||
|
|
||||||
|
ForceDeleteAction::make(),
|
||||||
|
|
||||||
|
RestoreAction::make(),
|
||||||
|
])
|
||||||
|
->toolbarActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
...DefaultBulkActions::make('Ayam'),
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->emptyStateIcon(Heroicon::RectangleGroup)
|
||||||
|
->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
|
||||||
|
->defaultSort('created_at', 'desc')
|
||||||
|
->deferFilters(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ManageChickens::route('/'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRecordRouteBindingEloquentQuery(): Builder
|
||||||
|
{
|
||||||
|
return parent::getRecordRouteBindingEloquentQuery()
|
||||||
|
->withoutGlobalScopes([
|
||||||
|
SoftDeletingScope::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Chickens\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Actions\Cheerful\CreateAction;
|
||||||
|
use App\Filament\Resources\Master\Chickens\ChickenResource;
|
||||||
|
use Filament\Resources\Pages\ManageRecords;
|
||||||
|
use Filament\Support\Enums\Width;
|
||||||
|
|
||||||
|
class ManageChickens extends ManageRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = ChickenResource::class;
|
||||||
|
|
||||||
|
protected static ?string $title = 'Ayam';
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
CreateAction::make()
|
||||||
|
->label('Tambah')
|
||||||
|
->modalHeading('Tambah Ayam')
|
||||||
|
->modalSubmitActionLabel('Simpan')
|
||||||
|
->modalCancelActionLabel('Batal')
|
||||||
|
->extraModalFooterActions(fn (CreateAction $action): array => [
|
||||||
|
$action->makeModalSubmitAction('createAnother', arguments: ['another' => true])
|
||||||
|
->label('Simpan dan Tambah Lagi'),
|
||||||
|
])
|
||||||
|
->modalWidth(Width::Large),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
24
app/Models/Chicken.php
Normal file
24
app/Models/Chicken.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\ChickenStatus;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class Chicken extends Model
|
||||||
|
{
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'hatch_date' => 'date',
|
||||||
|
'arrival_date' => 'date',
|
||||||
|
'exit_date' => 'date',
|
||||||
|
'status' => ChickenStatus::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Enums\ChickenStatus;
|
||||||
|
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('chickens', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('tag_code', 20);
|
||||||
|
$table->date('hatch_date')->nullable();
|
||||||
|
$table->date('arrival_date');
|
||||||
|
$table->date('exit_date')->nullable();
|
||||||
|
$table->enum('status', ChickenStatus::cases())->default(ChickenStatus::ACTIVE->value)->comment(ChickenStatus::comment());
|
||||||
|
$table->timestamp('created_at')->useCurrent();
|
||||||
|
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
|
||||||
|
$table->softDeletes();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('chickens');
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user