0

Title pretty much says it all, I'm exploring Filament v2 and trying to make a simple modal that creates a user, manipulates the password to be a random string, and then fires an email once saved. When I used this code on a normal resource, everything worked fine. However, when using a simple resource, I keep getting a "Property [$record] not found on component: [app.filament.resources.user-resource.pages.manage-users]" error.

The code in my ManageUser.php file is as follows:

<?php

namespace App\Filament\Resources\UserResource\Pages;

use Filament\Pages\Actions;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\Registered;
use App\Filament\Resources\UserResource;
use Filament\Pages\Actions\CreateAction;
use Filament\Resources\Pages\ManageRecords;
use Illuminate\Auth\Notifications\ResetPassword;

class ManageUsers extends ManageRecords
{
    protected static string $resource = UserResource::class;

    protected function getActions(): array
    {
        return [
            CreateAction::make()
                ->mutateFormDataUsing(function (array $data): array {
                    $data['password'] = Hash::make(Str::random(30));

                    return $data;
                })
                ->after(function(array $data) {
                    dd($this->record);
                    $token = Str::random(35);

                    DB::table('password_reset_tokens')->insert([
                        'email' => $this->record->email,
                        'token' => bcrypt($token),
                        'created_at' => now(), 
                    ]);

                    event(new Registered($this->record));
                    $this->record->notify(new ResetPassword($token));
                })
        ];
    }
}

enter image description here

I'm a bit lost as to what the issue is, have I missed a step?

1 Answer 1

0

This was an easy one that I'd overlooked, I thought the $this->record was inherited like it is in the standard resources, but in the simple resources you have to inject it into the function like this:

->after(function(Model $record) {
    $token = Str::random(35);

    DB::table('password_reset_tokens')->insert([
        'email' => $record->email,
        'token' => bcrypt($token),
        'created_at' => now(), 
    ]);

    event(new Registered($record));
    $record->notify(new ResetPassword($token));
})

Everything works as expected now

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.