pass additional parameters to Laravel Eloquent API Resources

  laravel

I’m working with laravel api Resources and badly need to pass some additional parameters to api Resources

here i am pasting the code of my controller:

return response()->json([
   'countries' => CountryResource::collection($countries, 'en')
], 200);

here with the collection i am passing dummy data which i need in the Resource, remember there will be multiple numbers of parameters which needed to be passed to Resource.

Now let me paste the code of my Resource

private $language;

public function __construct($resource, $language)
{
    // Ensure you call the parent constructor
    parent::__construct($resource);
    $this->resource = $resource;

    $this->language = $language;
}


public function toArray($request)
{
    return [
        'language' => $this->language,
        'id' => $this->id,
        'name' => $this->name,
    ];
}

I am getting the data here in “language” variable but the issue is i’m not getting what i am passing it’s showing me integers like “0”,”1″ like this but i have passed “en”

i have tried many solutions from stackoverflow but not worked for me

Source: Laravel StackOverflow

Leave a Reply