Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thestevemcniven's avatar

How do I turn a MediaLibrary object into just an Array of name, url for Inertia?

So this works, I get the related just fine into Inertia

Testimonial::with("media")->find(3);

The problem is it's the full bloated media object, when all I need is just the name and url in the array of items. Is it possible to filter?

Testimonial::with("media:name")->find(3);

//or

Testimonial::with(['media' => function ($query) {
                                                $query->select('name');
                                            }])->find(3);

I tried this suggestion, but doesn't return anything, where without :name it returns 2 items. Any time I try to mess with the data coming back it just returns me nothing

0 likes
1 reply
Niush's avatar

Also try by including id column.

Testimonial::with("media:id,name")->find(3);

Please or to participate in this conversation.