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
Please ask about the actual problem you're trying to solve, instead of your attempted solution. https://xyproblem.info
No , not working with me,
can you share the code please?
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
$this->record ?
Not work, keep in mind we are inside static method and we cannot called $this inside
$livewire->getRecord()
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.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.
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?
right, but if you're doing this in a resource then the same form gets used for both Edit and Create.
my case is only for editing, I don't have to create
You can
function form($form)
in your EditRecord
class.could u explain more ? @John
Well, you have a resource, with an EditMyResourceName class, right?
yeah
just define the form there, instead of on the resource if you don't use a CreateMyResourceName class
Put a
protected function form($form)
in there, where you can $form->schema() etc.then you'll have access to the record
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
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:
I made it a separate function to not repeat myself, but you could simplify.
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.
Btw, I actually still don't fully understand what you are trying to achieve.
@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.
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?
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
(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?
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 worksHmm, not sure. Can you debug what's inside $data?
$here data is instance record
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?
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
Sure, np. Good luck!