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

aurelianspodarec's avatar

Filter query breaks image loading with Spatie

Hi there!

So I have an issue where the images won't load with Spatie Methods, when the code below is uncommented:

 $theme = $filters['theme'] ?? 'light';
        $device = $filters['device'] ?? 'desktop';

        $query->with('imageVariations', function($query) use ($theme, $device) {
            $query->join('site_variations', 'site_variation_id', 'site_variations.id')
                        ->where('site_variations.theme', $theme)
                        ->where('site_variations.device', $device);
        })->get();

Now, the code does work as expected, its not broken as the filtering does work.

The issue is, when that is uncommented, my image from spatie won't load, nothing gets loaded. But when that code is commented, the images load(bare in mind I said images, but the query above makes it so only one image will load).

The code above is used here:

class PageController extends Controller
{
    public function index()
    {
        return view('web.inspiration.pages.index', [
            'pages' => SitePage::latest()->filter(
                request(['color', 'page', 'device', 'theme'])
            )->paginate(10)->withQueryString()
        ]);
    }
}

The code this is used, is here:

<img src="{{ asset($props->imageVariations[0]->getFirstMediaUrl('image')) }}" />

And the URL loads, that the asset loads most likely, but I can't get first media URL, that doesn't load.

You might ask, well, is there anything in the imageVariations[0] - yes, the correct data, which has this model:

class SitePageImage extends Model implements HasMedia
{

    // TODO: Renamte this to SitePageVariation
    
    use HasFactory;
    use InteractsWithMedia;
    
    public function variation()
    {
        return $this->belongsTo(SiteVariation::class, 'site_variation_id');
    }

    public function registerMediaConversions(Media $media = null): void
    {
        $this
            ->addMediaConversion('preview')
            ->width(100)
            ->height(100);
    }

}

So I'm not sure how or why the spatie functions here won't load, ONLY when I have my filtering query uncommented.

I was thinking maybe to do with eager loading or something but I guess that's, not the case, so its something else most likely it seems.

0 likes
2 replies
aurelianspodarec's avatar

I feel like, that's because I'm not accessing the model, so I can't use the method.

Since the above loads the Page and that there eager loads PageImages the PageImage is JSON, so I'm trying to use method by trying to select JSON.

So when I do $props->imageVariations[0]->getFirstMediaUrl('image')) - that makes no sense, its like if I'm trying to access a value called getFirstMediaUrl or either some method in JSON?

SO I feel like I need to create a new method in the model for PageImage? And use polymorphic relationship to get the 'thumbnail' or whatever, I think.

Not sure, something like this anyway I think.

I get this when I output imageVarations[0]

{"id":3,"site_page_id":514,"site_variation_id":3,"image":"https:\/\/i.imgur.com\/mjZn29x.png","created_at":"2022-06-06T12:27:35.000000Z","updated_at":"2022-06-06T12:27:35.000000Z","device":"desktop","theme":"light"}

Which is what I want, and then the other part is included, but in that case, I need to also get or pre-fetch the image on request or something I think - the image filed there needs to be removed, as I store images in media tables now.

So I guess that's the issue with eager loading the variations, is that then I can't tell the variation JSON, to load its image

Please or to participate in this conversation.