5

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

1

3 Answers 3

2

Welcome to stack overflow. You are on the right track but you forgot to add id and name to your __construct. It should look like this


private $language;
private $id;
private $name;

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

    $this->language = $language;
    $this->id = $id; 
    $this->name = $name;
}


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

Sign up to request clarification or add additional context in comments.

8 Comments

I recommend passing an array to constructor so there will be no params order confusion: __construct(array $params) and retrieve them using $params['..']
@junaid id and name are coming through the $country array i have provided to the collection
@user8555937 buddy i have to get the value of language which i am passing the through the controller let me explain i am passing 'en' from the controller and getting 0,1 in the resource i want to get 'en' in the resource
@develoerzone here CountryResource::collection($countries, 'en') you are passing en to the constructor of yourCountryResource which is why you are getting the language en. You need to explain what should be in the id and name of your resource.
@junaidrasheed i am not getting 'en' in the resource in i'm getting integers like 0,1 i just need to get the value of language what i am sending from the controller
|
0

Inside your model modify the code to this instead:

protected $language;

# appends language attribute to array
protected $appends = [
    'language'
];

# get language using accessor
public function getLanguageAttribute()
{
    return $this->language;
}

Comments

0

It can be achieved by mapping the model collection:

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

Under the hood, the base Laravel class uses tap().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.