14

In Laravel it can be done as simply as it is described here: https://laravel.com/docs/5.6/eloquent-resources.

Some says, API Resources is not meant for Lumen. However, just for the sake of this question, I want to know, strictly, if there is a way on how to add Laravel JSON API Resource in a Lumen project (the package use Illuminate\Http\Resources\Json\JsonResource; is missing from freshly created Lumen project).

notalentgeek
  • 4,939
  • 11
  • 34
  • 53

3 Answers3

27

API Resources are available in lumen, the files are there under: vendor\illuminate\http\Resources. what's missing is the artisan command to generate them. So just create the files manually, something like: app\Http\Resources\UserResource.php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'name' => $this->name,
            'email' => $this->email,
        ];
    }
}

I don't know who says, API Resources is not meant for Lumen, but that's not true.

Rabah
  • 2,052
  • 2
  • 16
  • 20
  • 3
    For Lumen 5.8, if you add the package flipbox/lumen-generator: ```composer require flipbox/lumen-generator``` you will be able to use it with the artisan command ```artisan make:resource myresource``` – Osh Mansor Jan 20 '20 at 09:21
  • there is anything about resource in lumen docs, so thanks for the reply – JahStation Dec 22 '21 at 15:14
-4

if you want to send response in json then use in your controller return response($res, 200); here 200 is stauts code

-4

Upgrade your laravel to 6.xx run

composer install

then only you see the Resource classes under

vendor/illuminate/http/ directory

Hussain Rahimi
  • 802
  • 8
  • 19