0

I am trying to eager load all the logotype urls from Spatie's laravel-medialibrary:

They are then going to used a json-response.

Ideally I the library would work as follow:

# Get all clients with url to logotype (small)`
    Client::with('media.logotype', function ($q) {
        $q->where('mediaConversion', 'small')->getUrl();
    })->get()



# Get all clients with all urls for media:logotype    
Client::with('media.logotype', function ($q) {
       $q->getUrl();
    })->get()

Ideas?

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Line in Linus
  • 390
  • 8
  • 25

1 Answers1

0

I got the josn as it, then I create another variable container the URL, if it just on image then:

foreach ($doctors as $doctor)
    if ($doctor->image != null)
        $image_url = '';
        $image_url = $doctor->image->thumbnail;
        $doctor->image_url = $image_url;

and then I hide the original one from the Doctor model by adding image, media to the hidden array, (if there is not, then just create the variable in the model)

    protected $hidden = ['media', 'image'];

if you have more than one image you can do it like this:

        $images_url = [];
        foreach($portfolio->images as $images){
            $images_url[] = $images->url;
        }
        $portfolio->images_url = $images_url;

another example

    public function show($doctor_id)
    {
        $portfolios = Portfolio::where('doctor_id', $doctor_id)->get();
        foreach ($portfolios as $portfolio) {
            $images_url = [];
            foreach ($portfolio->images as $images) {
                $images_url[] = $images->url;
            }
            $portfolio->images_url = $images_url;
        }
        return new PortfolioResource($portfolios);
    }