Get current instance

Hi folks I would like to ask about how can I access current insurance of madel that I'm editing I don't wanna use before Saving or Callbacks Due I needs access Attributes before creating input because i needs to creating dynamic records
34 Replies
Filament
Filament17mo ago
Please ask about the actual problem you're trying to solve, instead of your attempted solution. https://xyproblem.info
LeandroFerreira
LeandroFerreira17mo ago
Filament
Advanced - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
i.musabah
i.musabahOP17mo ago
No , not working with me,
LeandroFerreira
LeandroFerreira17mo ago
can you share the code please?
i.musabah
i.musabahOP17mo ago
public static function form(Form $form): Form { list($locales, $keyNamespace, $keyGroup, $keyItem) = $translate->getTranslateParams(); foreach ($locales as $locale){ $fileds[] = Forms\Components\TextInput::make('value')->required(); } return $form ->schema($fileds); } here $translate should be a current instance -record - @Leandro Ferreira
awcodes
awcodes17mo ago
$this->record ?
i.musabah
i.musabahOP17mo ago
Not work, keep in mind we are inside static method and we cannot called $this inside squint
awcodes
awcodes17mo ago
$livewire->getRecord()
John
John17mo ago
inside ->afterStateHydrated() you can put a closure and access the current record, form state, and more. It's not static.Please read the docs that were provided by Leandro.
awcodes
awcodes17mo ago
I know there’s a way to do it. Will get back to you soon when I get to work. Ok, so in v2 this is going to be troublesome, in v3 you can call $form->getRecord(). But, I think this is going to cause you problems since this won't work on a Create page since there is no record there.
i.musabah
i.musabahOP17mo ago
So what is the best practice for this? public static function form(Form $form): Form { list($locales, $keyNamespace, $keyGroup, $keyItem) = $translate->getTranslateParams(); foreach ($locales as $locale){ $fileds[] = Forms\Components\TextInput::make($locale)->required(); } return $form ->schema($fileds); } here $translate should be a current instance -a record -😫 afterStateHydrated this work if they have the same name but in my case is different you can read my case first @awcodes BTY this edit not create how can access $livewire->getRecord() inside form?
awcodes
awcodes17mo ago
right, but if you're doing this in a resource then the same form gets used for both Edit and Create.
i.musabah
i.musabahOP17mo ago
my case is only for editing, I don't have to create
John
John17mo ago
You can function form($form) in your EditRecord class.
i.musabah
i.musabahOP17mo ago
could u explain more ? @John
John
John17mo ago
Well, you have a resource, with an EditMyResourceName class, right?
i.musabah
i.musabahOP17mo ago
yeah
awcodes
awcodes17mo ago
just define the form there, instead of on the resource if you don't use a CreateMyResourceName class
John
John17mo ago
Put a protected function form($form) in there, where you can $form->schema() etc.
awcodes
awcodes17mo ago
then you'll have access to the record
i.musabah
i.musabahOP17mo ago
aha, overwrite form function inside editResourceClass , I will try it now Oh its works, Thanks you Guys so I can overwriting and add new functions in this class
John
John17mo ago
Then, what you can do in a lot of functions, is make use of special attributes like $record, $state, $get. It's all in the docs that Leandro provided. An example from my code:
// inside schema() -->
TextInput::make('function')
->label(__('model.RequestSigner.function'))
->maxLength(255)
->required()
->lazy()
->afterStateUpdated(self::requestSignerAfterStateUpdated($signerType)),
// inside schema() -->
TextInput::make('function')
->label(__('model.RequestSigner.function'))
->maxLength(255)
->required()
->lazy()
->afterStateUpdated(self::requestSignerAfterStateUpdated($signerType)),
private function requestSignerAfterStateUpdated(SignerType $signerType): Closure
{
return function (?Model $record, Field $component, $state, $livewire) use ($signerType) {
if (!$record) {
$record = new RequestSigner([
'request_id' => $livewire->data['request_id'],
'signer' => $signerType->value,
]);
}
$record->{$component->getStatePath(false)} = $state;
$record->save();
};
}
private function requestSignerAfterStateUpdated(SignerType $signerType): Closure
{
return function (?Model $record, Field $component, $state, $livewire) use ($signerType) {
if (!$record) {
$record = new RequestSigner([
'request_id' => $livewire->data['request_id'],
'signer' => $signerType->value,
]);
}
$record->{$component->getStatePath(false)} = $state;
$record->save();
};
}
I made it a separate function to not repeat myself, but you could simplify.
awcodes
awcodes17mo ago
for context, a Resource is not directly used, it's really more of a "configuration" class for the Edit / Create / List classes. They all reference the Resource class. It's a way of not having to duplicate certain things across all three classes.
John
John17mo ago
Btw, I actually still don't fully understand what you are trying to achieve.
i.musabah
i.musabahOP17mo ago
@John I've wrote laravel package and I'm trying to integrate it with filamentPHP if that works I will start useing filamentPHP admin panel for my future project https://github.com/ibrahimMH13/translate
GitHub
GitHub - ibrahimMH13/translate: static text translate package for l...
static text translate package for laravel 6 up. Contribute to ibrahimMH13/translate development by creating an account on GitHub.
John
John17mo ago
So, your package provides an admin panel where users can provide translations for existing keys? And you want to rebuild that admin panel with Filament?
i.musabah
i.musabahOP17mo ago
yeah when the developer uses trans() or __ functions will automatically load this keys into Laravel app and create these keys into db by serviceprivder that give the admin or content team the ability to edit trans without back to the developers team I already use my package for many projects and its works with Laravel 6 and above. Recently found Fliamnt AND now I'm trying ingrate my package with it @John and I struggle with it
John
John17mo ago
(please don't @ tag people, see #✅┊rules , you can Reply on a previous message instead) It can be tough. I also started using Filament just recently. Couple of months now. But... it can do a lot, and where it doesn't provide out of the box, Livewire is there to help. Can you describe what exactly are you struggling with now?
i.musabah
i.musabahOP17mo ago
Hum, I already access the record and built inputs but here I facing another challenge with inputs name I use <input name="value[$local]" /> in normal html tag but how can present this in the filament input component? I wrote this
protected function form(Form $form): Form { list($locales, $keyNamespace, $keyGroup, $keyItem) = $this->record->getTranslateParams(); $fields = []; foreach ($locales as $locale => $data) { $fields[] = TextInput::make("value[$locale]") ->afterStateHydrated(function (TextInput $component, $state) use ($locale, $data) { $component->state(data_get($data, 'translation.value.'.$local)); }); } return $form->schema($fields); }
but not works
John
John17mo ago
Hmm, not sure. Can you debug what's inside $data?
i.musabah
i.musabahOP17mo ago
$here data is instance record
John
John17mo ago
Filament is built with the idea that every field maps with a record field. Or multiple related through Eloquent relation or json field. Can't you refactor the handling, where the form is submitted?
i.musabah
i.musabahOP17mo ago
Yeah, I think if I wanna ingrate my package with Filament I have to rebuild the structure of it. I just realize in Filament handle every column alone so I can't process multi data or records together. so that I will do that after I finish the current project , anyway I really appreciate your help and notes also all Guys here
John
John17mo ago
Sure, np. Good luck!
Want results from more Discord servers?
Add your server