laravellaravel-resource

Why using additive paramerer in Resource collection raised error?


In laravel 9 app I want to add additive paramerer into Resource and looking at this Laravel 5.6 - Pass additional parameters to API Resource?

branch I remade in app/Http/Resources/CurrencyResource.php :

<?php

namespace App\Http\Resources;

use App\Library\Services\DateFunctionalityServiceInterface;
use Illuminate\Http\Resources\Json\JsonResource;
use App;
use Illuminate\Support\Facades\File;
use Spatie\Image\Image;
use App\Http\Resources\MediaImageResource;

class CurrencyResource extends JsonResource
{
    protected $show_default_image = false;

    public function showDefaultImage($value){
        $this->show_default_image = $value;
        return $this;
    }


    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        $dateFunctionality = App::make(DateFunctionalityServiceInterface::class);
        $currencyImage = [];
        $currencyMedia = $this->getFirstMedia(config('app.media_app_name'));

        if ( ! empty($currencyMedia) and File::exists($currencyMedia->getPath())) {
            $currencyImage['url']        = $currencyMedia->getUrl();
            $imageInstance              = Image::load($currencyMedia->getUrl());
            $currencyImage['width']      = $imageInstance->getWidth();
            $currencyImage['height']     = $imageInstance->getHeight();
            $currencyImage['size']       = $currencyMedia->size;
            $currencyImage['file_title'] = $currencyMedia->file_name;
        }
        else {
            \Log::info(  varDump($this->show_default_image, ' -1 $this->show_default_image::') );
            $currencyImage['url']  = $this->show_default_image ? '/images/default-currency.jpg' : '';
        }
        //         $currencyMedia = $currency->getFirstMedia(config('app.media_app_name'));
        return [
            'id'        => $this->id,
            'name'      => $this->name,
        ...

    

and with code in control :

'currency' => (new CurrencyResource($currency))->showDefaultImage(false),

its work ok, but I got an error :

Method Illuminate\Support\Collection::showDefaultImage does not exist.

when I applyed this method for collection:

return (CurrencyResource::collection($currencies))->showDefaultImage(true);

But in link above there is a similar way :

UserResource::collection($users)->foo('bar');

What is wrong in my code and how that can be fixed ?

Thanks!


Solution

  • I wonder if there is a reason you can't use this approach: https://stackoverflow.com/a/51689732/8485567

    Then you can simply use the request parameters to modify your response.

    If you really want to get that example working in that way, it seems you are not following the example correctly.

    You need to override the static collection method inside your CurrencyResource class:

    public static function collection($resource){
       return new CurrencyResourceCollection($resource);
    }
    
    

    You also need to create the CurrencyResourceCollection class and define the showDefaultImage method and $show_default_image property on that class as in the example you referred to.

    Then you should be able to do:

    CurrencyResource::collection($currencies)->showDefaultImage(true);
    

    The reason the way you are doing it doesn't work is because you haven't defined the static collection method on your resource hence it's defaulting to the normal behavior of returning a default collection object as you can see in your error message.