Laravel-medialibrary: Eager load the url

Created on 15 Jan 2019  路  2Comments  路  Source: spatie/laravel-medialibrary

Fantastic package and outstanding quality. One remark. Is it possible to eager load the url?

collection_name: "images" created_at: "2019-01-15 15:35:00" custom_properties: [] disk: "public" file_name: "background.jpg" id: 1 manipulations: [] mime_type: "image/jpeg" model_id: 40 model_type: "App\Item" name: "background" order_column: 1 responsive_images: [] size: 247634 updated_at: "2019-01-15 15:35:00"

also the URL

Most helpful comment

you could define an accesor getUrlAttribute and add it to $appents variable. Than: the toArray() will return also this value

Thanks very much! That solved my problem!

I implemented a custom class as explained here:
https://docs.spatie.be/laravel-medialibrary/v7/advanced-usage/using-your-own-model

And then added the url as shown below:

<?php

namespace App;
use Spatie\MediaLibrary\Models\Media as BaseMedia;

class Media extends BaseMedia
{
    protected $appends = ['url'];

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

All 2 comments

you could define an accesor getUrlAttribute and add it to $appents variable. Than: the toArray() will return also this value

you could define an accesor getUrlAttribute and add it to $appents variable. Than: the toArray() will return also this value

Thanks very much! That solved my problem!

I implemented a custom class as explained here:
https://docs.spatie.be/laravel-medialibrary/v7/advanced-usage/using-your-own-model

And then added the url as shown below:

<?php

namespace App;
use Spatie\MediaLibrary\Models\Media as BaseMedia;

class Media extends BaseMedia
{
    protected $appends = ['url'];

    public function getUrlAttribute()
    {
        return $this->getUrl();
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

intrepidws picture intrepidws  路  3Comments

Krato picture Krato  路  4Comments

aaronfullerton picture aaronfullerton  路  4Comments

mohammad6006 picture mohammad6006  路  4Comments

brendt picture brendt  路  4Comments