Laravel Filament v2 – 使用简单资源时,$this->record不存在

huangapple go评论57阅读模式
英文:

Laravel Filament v2 - $this->record doesn't exist when using a simple resource

问题

标题基本上已经说明了一切,我正在探索Filament v2,并尝试创建一个简单的模态框,用于创建用户,将密码修改为随机字符串,然后保存后发送电子邮件。当我在普通资源上使用这段代码时,一切都正常工作。然而,当使用简单资源时,我不断收到一个错误信息:"在组件上找不到属性[$record]:[app.filament.resources.user-resource.pages.manage-users]"。

ManageUser.php文件中的代码如下:

<?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));
                })
        ];
    }
}

我对问题有点困惑,我是否错过了什么步骤?

英文:

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:

&lt;?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()
                -&gt;mutateFormDataUsing(function (array $data): array {
                    $data[&#39;password&#39;] = Hash::make(Str::random(30));

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

                    DB::table(&#39;password_reset_tokens&#39;)-&gt;insert([
                        &#39;email&#39; =&gt; $this-&gt;record-&gt;email,
                        &#39;token&#39; =&gt; bcrypt($token),
                        &#39;created_at&#39; =&gt; now(), 
                    ]);

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

Laravel Filament v2 – 使用简单资源时,$this->record不存在

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

答案1

得分: 0

这是一个我忽视了的简单问题,我以为$this->record会像标准资源那样被继承,但在简单资源中,你必须像这样将其注入到函数中:

->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));
})

现在一切都按预期工作了。

英文:

This was an easy one that I'd overlooked, I thought the $this-&gt;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:

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

    DB::table(&#39;password_reset_tokens&#39;)-&gt;insert([
        &#39;email&#39; =&gt; $record-&gt;email,
        &#39;token&#39; =&gt; bcrypt($token),
        &#39;created_at&#39; =&gt; now(), 
    ]);

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

Everything works as expected now

huangapple
  • 本文由 发表于 2023年8月9日 01:35:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定