0

I am trying to push an item status into array and then return through API Resource, but API resource returning error. I am trying following script to do that

$Games = Game::all();
return new GameResource($Games);

And it is return as following

{
    "data": [
        {
            "id": 1,
            "name": "similique",
            "type_id": 3,
            "created_at": "2018-10-30 11:23:27",
            "updated_at": "2018-10-30 11:23:27"
        }
    ]
}

I am trying following to achieve my desire json array

$Games = Game::all();
$DataArray = ['status' => 'success', 'data' =>$Games ];
return new GameResource($DataArray);

But it is returning error

Call to a member function toBase() on array in fil

My desire json array is following

{
    "status": "success",
    "data": [
        {
            "id": 1,
            "name": "similique",
            "type_id": 3,
            "created_at": "2018-10-30 11:23:27",
            "updated_at": "2018-10-30 11:23:27"
        }
    ]
}
4
  • The complete error message should say it all: You are calling a method on an array. You should post the relevant code of that section as there is no toBase() method here. Commented Nov 13, 2018 at 11:51
  • @jeroen so I need to make an override method? Commented Nov 13, 2018 at 12:02
  • Could you also provide the code for the GameResource class? Commented Nov 13, 2018 at 12:02
  • @SvenHakvoort trust me there is nothing except laravel created API Resource Commented Nov 13, 2018 at 12:03

4 Answers 4

1

In your GameResource.php change the toArray() method:

/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function toArray($request)
{
    return [
        'data' => $this->collection,
        'status' => 'success', // Here goes the logic which checks for success or failure. However, this depends on what you consider as "success".
   ];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much, but actually problem is I am using same GameResource.php for lot of API's. So I need a generic solution or is there any way to without changing anything in GameResource.php?
When you change it inside GameResource.php you will have the change on all usages of this resource. How can it be more generic?
You are absolutely right, that's why I am trying different approach as like to fix in controller or if you have any other solution then kindly guide me. I appreciate for this answer and voted up +1
I guess, I don't understand your usecase. If you have the case that some game resources need a status flag and some not, you have different resources. This means you need different Resource classes.
1

You can try this code:

$games = Game::get();

return response()->json(new GameResource($games));

1 Comment

Can you just read the question properly before posting an answer. It is so weird
0

data is a public property of your GameResource ? then your code should be

return ['status' => 'success', 'data' => (new GameResource($Games))->data ];

Comments

0

Brother, I am using this way.

Step 1: I create a generalOutput Resource.php and modify like below.

class GeneralOutputResource extends JsonResource
{
    private $status;
    private $data;
    public function __construct($status, $data)
    {
        $this->status = $status;
        $this->data = $data;
    }
    public function toArray($request)
    {
       return ['status' => $this->status, 'data' => $this->data];
    }
}

Step 2: Then I just call the class like this

 $a_user = User::find(1);
 return new GeneralOutputResource(1, $a_user);
  • Everytime I return the response, I will call the GeneralOutputResponse.

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.