Access $record in resource
Hi, I'm trying to get the data from the database row I'm working on using resources, in this case ProjectResource, I tried to get it with mount(), but I can't get it, how can I do it?
23 Replies
On what page?
And how does your code look like.
In List/Edit/View mode you can use https://filamentphp.com/docs/3.x/tables/columns/advanced#injecting-the-current-eloquent-record
I know, in view mode I can use it, but I want to get it in edit resource, when creating a form.
So on the Create and Edit form you want it?
Because i think the Resource page is not a single record page.
Only in edit
You can’t do that in the Resource class. You need to do it in the EditProject class.
And it’s probably $this->getRecord() or $this->record, make sure to use parent::mount() in your mount function though otherwise it won’t work.
This is the function defined in the parent by default in EditRecord, should I overwrite it?
You can if you call parent::mount() first in your override.
Question is, is that the appropriate place for your needs. I’m still not sure exactly what your goal is. 😀
Maybe for my purpose, this is not necessary then, I in my case, what I do in the Resource form, both to create and to edit, is to create an own Hexadecimal Id, without repeating it, but every time I update it changes, so I want to check the id (id that is created with the table) so that it does not generate it in case of editing
Sorry, not following.
I generate an id, which is not the same as the id of the table, but has to be unique. So, what I wanted to do is to get a $record of my project when editing, so that it doesn't generate it for me. And in case it is Create, it will create it for me.
Something feels off here. Wouldn’t $record be the project?
So why do you need an id before it’s even created. The form lifecycle hooks might be a better place to handle this.
Also hidden fields for anything id related is probably a bad idea since it can be manipulated on the frontend.
I don't know what you mean with that
As for the other thing, I know it's not a good idea, but for this particular project it's an id that is not as sensitive as the table id
I thought that if I wanted to create a function with its own ID it would have to be there
Maybe that’s where I’m getting confused. What is the difference between the two ids?
What is the need for a secondary identifier?
It is the same, but the identifier is one of its own, as if it were a name
Where should I create it from otherwise, being from the resources form where I create the form?
I don’t see why it’s needed.
Everything in the system should just reference its table id.
Even if that is is a uuid or ulid.
What do you actually need the secondary id for?
Where is it applicable or needed in order to reference the record.
It's really like a name identifier, but generated automatically, and with hexadecimal
Still, why does it need a name identifier? Maybe that’s what I’m getting at.
This is something that the client has asked me to do in order to make it more comfortable for him
So wouldn’t that just be a column on the record in the db? Then you can set it when the record is actually created. It shouldn’t change after that. And you can use the create life cycle hooks to do that or an observer might be better.
Ok, I'll try it the other way
Thank you very much for your help and patience 😄