flipboxstudio / lumen-generator

A Lumen Generator You Are Missing
https://packagist.org/packages/flipbox/lumen-generator
MIT License
824 stars 126 forks source link

resource.stub should extend JsonResource? #86

Closed blestab closed 4 years ago

blestab commented 4 years ago

php artisan make:resource User created a resource with the following code for me:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class User extends Resource
{
...

When i hooked my controller, resource and model together like so:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Http\Resources\User as UserResource;

class UserController extends Controller
{
...
    public function index()
    {
        // fetch all users
        $users= Patient::paginate(15);

        // return userscollection
        return UserResource::collection($users);
    }
....

and when i did a GET request for /api/users , I get a 500 server error with message 'Class 'App\Http\Resources\Resource' not found in \ApiTutorial\app\Http\Resources\User.php on line 7'

I manually changed my resource to extend JsonResource instead of Resource and this resolved the issue for me. I also then fixed resource.stub and recreated my resource and that also worked as expected. I could submit a PR is that's okay?

Here is a snippet of my composer.json in case that is needed:

    "require": {
        "php": "^7.2.5",
        "flipbox/lumen-generator": "^6.0",
        "fruitcake/laravel-cors": "^1.0",
        "laravel/lumen-framework": "^7.0",
        "tymon/jwt-auth": "dev-develop"
    },