125 lines
4.4 KiB
PHP
125 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\User\Resources;
|
|
|
|
use App\Filament\User\Resources\RecipeResource\Pages;
|
|
use App\Models\Recipe;
|
|
use App\Models\RecipeSpice;
|
|
use App\Models\Spice;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\Repeater;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use App\Rules\SumEquals100;
|
|
|
|
class RecipeResource extends Resource
|
|
{
|
|
protected static ?string $model = Recipe::class;
|
|
protected static ?string $navigationIcon = 'heroicon-o-book-open';
|
|
protected static ?string $navgationLabel = 'Rezepte';
|
|
protected static ?string $modelLabel = 'Rezepte';
|
|
protected static ?string $slug = 'rezept';
|
|
protected static ?string $navigationGroup = 'Ressourcen';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
TextInput::make('name')
|
|
->required()
|
|
->label('Name'),
|
|
Textarea::make('description')
|
|
->label('Beschreibung'),
|
|
Repeater::make('spices')
|
|
->schema([
|
|
Select::make('spice_id')
|
|
->label('Rohstoff')
|
|
->options(Spice::all()->pluck('name', 'id'))
|
|
->searchable()
|
|
->required(),
|
|
|
|
TextInput::make('amount_in_percent')
|
|
->label('Anteil (in Prozent)')
|
|
->numeric()
|
|
->required()
|
|
->rules(['min:1', 'max:100']),
|
|
])
|
|
->label('Inhalt')
|
|
->columns(2)
|
|
->addActionLabel('Inhaltsstoff hinzufügen')
|
|
->afterStateHydrated(function ($state, $set, $record) {
|
|
if ($record) {
|
|
// Load the spices relationship with pivot data
|
|
$record->load('spices');
|
|
|
|
// Map the spices and their pivot data to the format the repeater expects
|
|
$spicesData = $record->spices->map(function ($spice) {
|
|
return [
|
|
'spice_id' => $spice->id,
|
|
'amount_in_percent' => $spice->pivot->amount_in_percent,
|
|
];
|
|
})->toArray();
|
|
|
|
// Set the state for the repeater field to the mapped spices data
|
|
$set('spices', $spicesData);
|
|
}
|
|
return $state;
|
|
})
|
|
->rules([new SumEquals100()]),
|
|
]);
|
|
}
|
|
/*
|
|
*/
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
TextColumn::make('name')
|
|
->label('Name')
|
|
->sortable()
|
|
->searchable(),
|
|
TextColumn::make('description')
|
|
->label('Beschreibung')
|
|
->limit(50),
|
|
TextColumn::make('spices.name')
|
|
->label('Inhalt')
|
|
->formatStateUsing(function ($state, $record) {
|
|
$spices = $record->spices->take(3)->map(function ($spice) {
|
|
return $spice->name . ' (' . round($spice->pivot->amount_in_percent) . '%)';
|
|
});
|
|
|
|
if ($record->spices->count() > 3) {
|
|
$spices->push('... weitere');
|
|
}
|
|
|
|
return $spices->implode(', ');
|
|
})
|
|
->limit(50),
|
|
]);
|
|
}
|
|
|
|
public static function getRecord($id) {
|
|
return Recipe::with('spices')->findOrFail($id); // Eager load spices relationship
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListRecipes::route('/'),
|
|
'create' => Pages\CreateRecipe::route('/create'),
|
|
'edit' => Pages\EditRecipe::route('/{record}/edit'),
|
|
];
|
|
}
|
|
public static function getPluralLabel(): string
|
|
{
|
|
return 'Rezepte';
|
|
}
|
|
}
|