Remove standalone RecipeResource — recipes managed via product page
Deploy / deploy (push) Successful in 9s
Deploy / deploy (push) Successful in 9s
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
a104924add
commit
7dfd01c11a
4 files changed
-203
No files matched your search
@@ -1,125 +0,0 @@
|
||||
<?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 = 'Produkte';
|
||||
protected static ?int $navigationSort = 2;
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\User\Resources\RecipeResource\Pages;
|
||||
|
||||
use App\Filament\User\Resources\RecipeResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\RecipeSpice;
|
||||
|
||||
class CreateRecipe extends CreateRecord
|
||||
{
|
||||
protected static string $resource = RecipeResource::class;
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
$record = static::getModel()::create($data);
|
||||
|
||||
foreach ($data['spices'] as $spice) {
|
||||
RecipeSpice::create([
|
||||
'recipe_id' => $record->id,
|
||||
'spice_id' => $spice['spice_id'],
|
||||
'amount_in_percent' => $spice['amount_in_percent'],
|
||||
]);
|
||||
}
|
||||
return $record;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
namespace App\Filament\User\Resources\RecipeResource\Pages;
|
||||
|
||||
use App\Filament\User\Resources\RecipeResource;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditRecipe extends EditRecord
|
||||
{
|
||||
protected static string $resource = RecipeResource::class;
|
||||
|
||||
protected function afterSave(): void
|
||||
{
|
||||
$this->syncSpices();
|
||||
}
|
||||
|
||||
protected function syncSpices(): void
|
||||
{
|
||||
$recipe = $this->record;
|
||||
|
||||
$spices = collect($this->data['spices'] ?? [])
|
||||
->mapWithKeys(function ($spice) {
|
||||
return [
|
||||
$spice['spice_id'] => ['amount_in_percent' => $spice['amount_in_percent']],
|
||||
];
|
||||
})
|
||||
->toArray();
|
||||
|
||||
$recipe->spices()->sync($spices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\User\Resources\RecipeResource\Pages;
|
||||
|
||||
use App\Filament\User\Resources\RecipeResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListRecipes extends ListRecords
|
||||
{
|
||||
protected static string $resource = RecipeResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user