Show First image: SpatieMediaLibraryImageColumn
How can I limit the "thumb" image in my table to the first image only when my record has more than one Spatie Media records associated?
I am using SpatieMediaLibraryImageColumn and always have several media images related...but they are cluttering up the table. I want to show the first one only.
I thought I could directly fetch what I need (pseudo-code):
->getStateUsing(fn($record) => $record->getFirstMedia()...)
But, it appears that ->getStateUsing()
is ignored completely8 Replies
Solution
try:
->limit(1)
just found that!
It works perfectly, thanks
Welcomes!
Is there a reason ->getStateUsing() doesn't work? in my case I'm all good...for now, but what if I wanted to get a bit more choosy with which image shows...
As in pick a different image based on some values I have stored in the
custom_properties
field of the table.Yeah, its a specific field type that doesn't have support for it. If you were converting the image properly you can set the conversion that you want to call too.
I was thinking I could get clever and use field: collection_name to isolate an image...but that feels a bit hacky to me.
So do that with:
->collection()
indeed.
Since all I need is the first image (luckily) I'm all good with ->limit(1) for now
thank you