laravel-medialibrary icon indicating copy to clipboard operation
laravel-medialibrary copied to clipboard

Bug: server fault when eager loading the media relation by default

Open jorenvh opened this issue 2 years ago • 1 comments

Laravel version: v9.9.0 Media library version: 10.3.2

I'm currently upgrading a Laravel 8 application running on php 7.4 to Laravel 9 on php 8.1. And I'm encountering failing tests and I was able to pinpoint the issue to the media library (so far).

I'm already creating an issue as I'm only working sporadic on this project in case anyone else is experiencing the same issue and has found a solution already. I will definitely look further into it myself as well and PR a fix when applicable.

The model that uses the Media Library eager loads the media relation by default as we will always need the attached media files and want to prevent having n + 1 queries. A cleaned implementation of the model can be found below.

<?php


namespace App\MyProject\MyDomain\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class MyModel extends Model implements HasMedia
{
    use InteractsWithMedia;

    // ....

    protected $with = ['media'];

    // ....
}

Documentation: https://laravel.com/docs/9.x/eloquent-relationships#eager-loading-by-default

In our test suite we have the following code, which results in a server fault

$myModel = MyModel::first();
$myModel->toArray();

I dug in and found that whenever I remove protected $with = ['media']; the code just works. So I went in deeper and found out that it fails on protected $appends = ['original_url', 'preview_url']; in the Media model. Whenever I remove original_url from the appends array the server fault is gone.

The strange thing is, when I eager load it manually on the query level, it just works.

I haven't figured out yet why it happens, neither do I have a fix for it yet. I will dive in further when I continue my work on this application. In the meantime I was hoping someone else encountered something similar and has found a solution for the issue.

jorenvh avatar Apr 21 '22 16:04 jorenvh

I've looked further into this but wasn't able to find a solution yet. Is anyone else running into similar issues?

jorenvh avatar Jun 14 '22 14:06 jorenvh

I have a similar issue, coming from upgrading a laravel project and upgrading Spatie/MediaLibrary from v7 to v9

<?php

namespace App\Models;

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;


class Media extends BaseMedia implements HasMedia
{
    use InteractsWithMedia{
        getFirstMediaUrl as protected getFirstMediaUrlTrait;
    }

    protected $appends = [
        'url',
        'thumb',
        'icon',
        'formated_size'
    ];

    /**
     * to generate media url in case of fallback will
     * return the file type icon
     * @param string $conversion
     * @return string url
     */
    public function getFirstMediaUrl($conversion = '')
    {
        $url = $this->getUrl();
        $array = explode('.', $url);
        $extension = strtolower(end($array));
        if (in_array($extension,config('media-library.extensions_has_thumb'))) {
            return asset($this->getUrl($conversion));
        }else{
            return asset(config('media-library.icons_folder').'/'.$extension.'.png');
        }
    }

    public function getUrlAttribute()
    {
        return $this->getFullUrl();
    }

    public function getThumbAttribute()
    {
        return $this->getFirstMediaUrl('thumb');
    }

    public function getIconAttribute()
    {
        return $this->getFirstMediaUrl('icon');
        
    }

    public function getFormatedSizeAttribute()
    {
        return formatedSize($this->size);
    }
}

This is my custom Media Class, Any Advices?

engmsaleh avatar Sep 22 '22 20:09 engmsaleh

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

spatie-bot avatar Mar 22 '23 11:03 spatie-bot