Reusable sections
Hi, I was wondering how you usually create reusable parts for example e.g. infolists or forms? For example I create my own
AddressEntry
that I am using on multiple resources. The problem comes when I need to access the Address model.
This is my demo custom entry:
Model in my closure in url method is taken from resource, when I use it on UserResource, it is User etc.
It kinda works, but I don't like this implementation. Do you know better way?14 Replies
What exactly is your problem with your implementation? Why don't you like it?
One point for me is, that the wording doesn't make sense in a laravel context. In most cases in laravel, a static
make
method would create an instance of the current class (in this case AddressEntry
). All Filament classes behave like that, the Model::make does it etc. Your code created not an instance of AddressEntry but an instance of TextEntry. So I think its a bad idea to use the word make
. I would suggest to be more generic and use this class for other common fields/entries, otherwise you would have to create separete classes for every szenario.
Just my two cents 😉I really appreciate your comments. But could you be more specific?
I don't know your exact scenario so it is a bit hard to be very specific, but I will try to explain it with FormFields which I centralize in my own projects. For this, I either create a class
CommonField
where I put static methods in for common used fields, even if there is not that much logic on the field. For example:
This is of course a matter of taste, but I like it this way, because (for example) all fields with currency values in my project to have the "€" postfix and be numeric, but I don't wanna create for a 3-liner an extra class/file.
Of course there are more complex ones, with callbacks 😉
I hope that helps. But as I said, this is a matter of tasteThanks, now I understand what you mean. My example was simplified to make it more clear. I understand your comments, but the problem was more related to the
->url
section. I will give you another example where I have extracted multiple fields into one array. My question is about the ->url
section. I don't like what I created in closure, but it works.
I use this array inside my ViewContract
and ViewEntity
pages.Ok, but why don't you like it? I don't get the point? That your argument
$record
is a Model
instead of User
?
If you would be lazy, you wouldn't even need a type at all 😄I use it like this:
You could move the callback back to the Resource:
It is not about types. It is about that how to access that
business_no
attribute. For example when I have this inside my UserResource closure is like this: fn ($record) => $record->entity->business_no
, but when it is inside ContractResource it is like this: fn ($record) => $record->client->entity->business_no
.And use it like:
depending on the resource
Or just accept that Entity model, that holds business_no, right? Maybe I was kind of hoping that there's some way to get to the Entity model even when I'm on a UserResource or ContractResource without having to push the model there directly.
Well or another way would be to implement it on the model itself and just call it then
so you would implement the method
getBusinessNo()
on both the Contract
and the Entity
modelI will probably do this:
Because I am doing more actions with this model inside that CompanyInfolist, not just one URL.
ok, but this would still work, just without the callback:
or
Btw. if you keep the origional version, I would do something with dublication of the url-string. for example:
because in your example, you have trippled the 'https://orsr.sk/hladaj_ico.asp?ICO='
or the new cleaner version:
Right, I understand. Thank you very much.
If you still have a moment, you could take a look at my other problem I'm having.
https://discord.com/channels/883083792112300104/1214858935425703958