3333use Filament \Forms \Components \DateTimePicker ;
3434use Filament \Forms \Components \Select ;
3535use Filament \Forms \Components \TextInput ;
36+ use Filament \Schemas \Components \Utilities \Get ;
37+ use Filament \Schemas \Components \Utilities \Set ;
38+ use Illuminate \Support \Str ;
3639
3740class ProductResource extends Resource
3841{
@@ -46,33 +49,42 @@ public static function form(Schema $schema): Schema
4649 {
4750 return $ schema
4851 ->components ([
49- TextInput::make ('name ' )
50- ->required ()
51- ->maxLength (255 ),
52- Textarea::make ('description ' )
53- ->maxLength (65535 ),
54- TextInput::make ('price ' )
55- ->required ()
56- ->numeric ()
57- ->prefix ('$ ' ),
58- Select::make ('category_id ' )
59- ->label ('Category ' )
60- ->options (ProductCategory::all ()->pluck ('name ' , 'id ' ))
61- ->searchable (),
62- TextInput::make ('inventory_count ' )
63- ->required ()
64- ->numeric ()
65- ->minValue (0 ),
66- TextInput::make ('low_stock_threshold ' )
67- ->required ()
68- ->numeric ()
69- ->minValue (0 )
70- ->label ('Low Stock Threshold ' ),
71- Select::make ('tags ' )
72- ->multiple ()
73- ->relationship ('tags ' , 'name ' )
74- ->preload (),
52+ Section::make ('Basic Information ' )
53+ ->columnSpanFull ()
54+ ->columns (2 )
55+ ->schema ([
56+ TextInput::make ('name ' )
57+ ->required ()
58+ ->maxLength (255 )
59+ ->live (onBlur: true )
60+ ->afterStateUpdated (function (Set $ set , Get $ get , ?string $ old , ?string $ state ): void {
61+ $ currentSlug = (string ) ($ get ('slug ' ) ?? '' );
62+ $ oldNameSlug = Str::slug ((string ) ($ old ?? '' ));
63+ if ($ currentSlug !== '' && $ currentSlug !== $ oldNameSlug ) {
64+ return ;
65+ }
66+ $ set ('slug ' , Str::slug ((string ) $ state ));
67+ }),
68+ TextInput::make ('slug ' )
69+ ->required ()
70+ ->unique (ignoreRecord: true )
71+ ->maxLength (255 ),
72+ Select::make ('category_id ' )
73+ ->label ('Category ' )
74+ ->options (ProductCategory::all ()->pluck ('name ' , 'id ' ))
75+ ->searchable (),
76+ Select::make ('tags ' )
77+ ->multiple ()
78+ ->relationship ('tags ' , 'name ' )
79+ ->preload (),
80+ Textarea::make ('description ' )
81+ ->maxLength (65535 )
82+ ->columnSpanFull (),
83+ ]),
84+
7585 Section::make ('Pricing ' )
86+ ->columnSpanFull ()
87+ ->columns (2 )
7688 ->schema ([
7789 Select::make ('pricing_type ' )
7890 ->options ([
@@ -81,51 +93,65 @@ public static function form(Schema $schema): Schema
8193 'donation ' => 'Pay What You Want ' ,
8294 ])
8395 ->default ('fixed ' )
84- ->reactive (),
85-
96+ ->live (),
8697 TextInput::make ('price ' )
8798 ->required ()
8899 ->numeric ()
89100 ->prefix ('$ ' )
90- ->visible (fn (callable $ get ) => $ get ('pricing_type ' ) === 'fixed ' ),
91-
101+ ->visible (fn (Get $ get ) => $ get ('pricing_type ' ) === 'fixed ' ),
92102 TextInput::make ('suggested_price ' )
93103 ->numeric ()
94104 ->prefix ('$ ' )
95- ->visible (fn (callable $ get ) => $ get ('pricing_type ' ) === 'donation ' ),
96-
105+ ->visible (fn (Get $ get ) => $ get ('pricing_type ' ) === 'donation ' ),
97106 TextInput::make ('minimum_price ' )
98107 ->numeric ()
99108 ->prefix ('$ ' )
100109 ->default (0 )
101- ->visible (fn (callable $ get ) => $ get ('pricing_type ' ) === 'donation ' ),
110+ ->visible (fn (Get $ get ) => $ get ('pricing_type ' ) === 'donation ' ),
102111 ]),
112+
113+ Section::make ('Inventory ' )
114+ ->columnSpanFull ()
115+ ->columns (2 )
116+ ->schema ([
117+ TextInput::make ('inventory_count ' )
118+ ->required ()
119+ ->numeric ()
120+ ->minValue (0 ),
121+ TextInput::make ('low_stock_threshold ' )
122+ ->required ()
123+ ->numeric ()
124+ ->minValue (0 )
125+ ->label ('Low Stock Threshold ' ),
126+ ]),
127+
103128 Section::make ('Downloadable Product ' )
129+ ->columnSpanFull ()
130+ ->columns (2 )
131+ ->collapsible ()
104132 ->schema ([
105133 Toggle::make ('is_downloadable ' )
106134 ->label ('Is Downloadable Product ' )
107- ->reactive (),
108-
135+ ->live ()
136+ -> columnSpanFull (),
109137 FileUpload::make ('downloadable_file ' )
110138 ->label ('Product File ' )
111139 ->disk ('local ' )
112140 ->directory ('downloadable_products ' )
113141 ->visibility ('private ' )
114142 ->acceptedFileTypes (['application/pdf ' , 'application/zip ' ])
115- ->maxSize (50 * 1024 ) // 50MB
116- ->visible (fn (callable $ get ) => $ get ('is_downloadable ' )),
117-
143+ ->maxSize (50 * 1024 )
144+ ->visible (fn (Get $ get ) => $ get ('is_downloadable ' ))
145+ -> columnSpanFull (),
118146 TextInput::make ('download_limit ' )
119147 ->label ('Download Limit ' )
120148 ->numeric ()
121149 ->minValue (1 )
122- ->visible (fn (callable $ get ) => $ get ('is_downloadable ' )),
123-
150+ ->visible (fn (Get $ get ) => $ get ('is_downloadable ' )),
124151 DateTimePicker::make ('expiration_time ' )
125152 ->label ('Download Expiration ' )
126- ->visible (fn (callable $ get ) => $ get ('is_downloadable ' )),
127- ])
128- ->collapsible (),
153+ ->visible (fn (Get $ get ) => $ get ('is_downloadable ' )),
154+ ]),
129155 ]);
130156 }
131157
0 commit comments