kiuyha
kiuyha
FFilament
Created by kiuyha on 2/4/2025 in #❓┊help
mutateFormDataBeforeCreate not working
I try to use mutateFormDataBeforeCreate to add records according to user input, but it seems this method is not being execute before creating record. I want it to make records as many as the user input the number.
class RoomResource extends Resource
{
protected static ?string $model = Room::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

protected static ?string $navigationGroup = 'Room Management';

public static function form(Form $form): Form
{
return $form
->schema([
Select::make('room_type_id')
->required()
->label('Tipe Kamar (ID)')
->options(Room_type::pluck('name_room_id', 'id')->toArray()),
TextInput::make('count')
->label('Jumlah Kamar')
->type('number')
->minValue(1)
->required()
->dehydrated(false)
]);
}

protected function mutateFormDataBeforeCreate(array $data): array
{
dd($data);
if (!isset($data['count']) || !is_numeric($data['count']) || $data['count'] <= 0) {
throw new \InvalidArgumentException('Jumlah kamar harus lebih dari 0.');
}
$count = (int) $data['count'];
$rooms = [];
for ($i = 0; $i < $count; $i++) {
$rooms[] = [
'room_type_id' => $data['room_type_id'],
'status' => 'Available',
'tenant' => null,
'created_at' => now(),
'updated_at' => now(),
];
}
Room::insert($rooms);
return [];
}
class RoomResource extends Resource
{
protected static ?string $model = Room::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

protected static ?string $navigationGroup = 'Room Management';

public static function form(Form $form): Form
{
return $form
->schema([
Select::make('room_type_id')
->required()
->label('Tipe Kamar (ID)')
->options(Room_type::pluck('name_room_id', 'id')->toArray()),
TextInput::make('count')
->label('Jumlah Kamar')
->type('number')
->minValue(1)
->required()
->dehydrated(false)
]);
}

protected function mutateFormDataBeforeCreate(array $data): array
{
dd($data);
if (!isset($data['count']) || !is_numeric($data['count']) || $data['count'] <= 0) {
throw new \InvalidArgumentException('Jumlah kamar harus lebih dari 0.');
}
$count = (int) $data['count'];
$rooms = [];
for ($i = 0; $i < $count; $i++) {
$rooms[] = [
'room_type_id' => $data['room_type_id'],
'status' => 'Available',
'tenant' => null,
'created_at' => now(),
'updated_at' => now(),
];
}
Room::insert($rooms);
return [];
}
6 replies
FFilament
Created by kiuyha on 2/2/2025 in #❓┊help
Make notifications for successful Reset password Email sent.
I wanna the email especially in reset password can provide the correct notifications, because the notifications when click sending email only show succesful sent email even though, it still being queue, I want it to give notifications according the job executing. I have event to listen to this event when job being success or fail like this.
class PasswordResetQueueListener
{
/**
* Handle job success.
* Especially for sending emails
*/
public function handleSuccess(JobProcessed $event)
{
if ($this -> isRequestPasswordReset($event)) {
// Notify user in Filament
Notification::make()
->title('Email telah dikirim, tolong cek inbox Anda')
->success()
->broadcast($this -> retrieveIdUser($event));
return false;
}
}

/**
* Handle job failure.
* Especially for sending emails
*/
public function handleFailure(JobFailed $event)
{
if($this -> isRequestPasswordReset($event)){
$notification = Notification::make()
->title('Gagal mengirim email.')
->body('Error: ' .$event->exception->getMessage())
->danger()
->broadcast($this->retrieveIdUser($event));
return false;
}
}

public function isRequestPasswordReset($event){
return $event->job->resolveName() === 'Filament\Notifications\Auth\ResetPassword';
}

public function retrieveIdUser($event){
$payload = $event->job->payload();
$user = unserialize($payload['data']['command'])->notifiables;
return $user;
}
}
class PasswordResetQueueListener
{
/**
* Handle job success.
* Especially for sending emails
*/
public function handleSuccess(JobProcessed $event)
{
if ($this -> isRequestPasswordReset($event)) {
// Notify user in Filament
Notification::make()
->title('Email telah dikirim, tolong cek inbox Anda')
->success()
->broadcast($this -> retrieveIdUser($event));
return false;
}
}

/**
* Handle job failure.
* Especially for sending emails
*/
public function handleFailure(JobFailed $event)
{
if($this -> isRequestPasswordReset($event)){
$notification = Notification::make()
->title('Gagal mengirim email.')
->body('Error: ' .$event->exception->getMessage())
->danger()
->broadcast($this->retrieveIdUser($event));
return false;
}
}

public function isRequestPasswordReset($event){
return $event->job->resolveName() === 'Filament\Notifications\Auth\ResetPassword';
}

public function retrieveIdUser($event){
$payload = $event->job->payload();
$user = unserialize($payload['data']['command'])->notifiables;
return $user;
}
}
But there's one problem which the broadcast will not work because the user is not have connection to channel when not log in. So I think to make temporary channel for it which make random uuid or smth like that and it need to pass in the job to being retrieve. Please help me, because this my first time work with php and laravel
7 replies
FFilament
Created by kiuyha on 1/30/2025 in #❓┊help
Ably broadcasting is not showing up
No description
4 replies