Giter Club home page Giter Club logo

Comments (8)

joselfonseca avatar joselfonseca commented on June 15, 2024 1

It actually does XD

  "links" => array:4 [
    "first" => "http://localhost/api/foo?page=1"
    "last" => "http://localhost/api/foo?page=2"
    "prev" => null
    "next" => "http://localhost/api/foo?page=2"
  ]
  "meta" => array:7 [
    "current_page" => 1
    "from" => 1
    "last_page" => 2
    "path" => "http://localhost/api/foo"
    "per_page" => 20
    "to" => 20
    "total" => 31
  ]
Route::group(['prefix' => 'foo'], function () {
        Route::get('/', 'Users\UsersController@index');
/**
     * Returns the Users resource with the roles relation.
     *
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $paginator = $this->model->with('roles.permissions')->paginate($request->get('limit', config('app.pagination_limit')));
        if ($request->has('limit')) {
            $paginator->appends('limit', $request->get('limit'));
        }

        return UserResource::collection($paginator);
    }

from laravel-api.

kslimani avatar kslimani commented on June 15, 2024

@joselfonseca "API evolution" is the methodology i already use for my applications.

By dropping Dingo, you lose the "mime based" ressource versioning feature, but you can still use "URL based" ressource versioning. (so I am not against dropping Dingo dependency).

About replacing Fractal by Eloquent Resources : i have not yet myself tested this feature. It seems powerfull but i don't know how it handle the pagination for collection, especially the links in meta.

Do you know if it is automatically handled by Laravel or do you have to generate the url links yourself ?
(if not, it may be required to create some helpers to reproduce the Dingo collection metadata)

About the OpenAPI spec i don't know, i think it may depends on the available tools to generate api documentation. (Aglio is pretty cool for API blueprint format).

from laravel-api.

joselfonseca avatar joselfonseca commented on June 15, 2024

@kslimani so the pagination is handled by API resources, won't be the same but close enough (https://laravel.com/docs/5.6/eloquent-resources#pagination)

OpenAPI has nice tools to generate docs as well. This is just something came to my mind for a future release TBH i am using a lot of GraphQL and Node this days so is not something i am dying for right now, just wanted to know your opinion.

from laravel-api.

kslimani avatar kslimani commented on June 15, 2024

I have read the pagination part for API resources, i was just wondering how the urls in "links" and "meta" are generated in the provided example :

// Copy paste from https://laravel.com/docs/5.6/eloquent-resources#pagination
use App\User;
use App\Http\Resources\UserCollection;

Route::get('/users', function () {
    return new UserCollection(User::paginate());
});

I guess i will have to performs some tests.

from laravel-api.

joselfonseca avatar joselfonseca commented on June 15, 2024

Yea, I was looking into that and got this:

[
  "data" => array:0 []
  "links" => array:4 [
    "first" => "http://localhost/api/users?page=1"
    "last" => "http://localhost/api/users?page=2"
    "prev" => null
    "next" => "http://localhost/api/users?page=2"
  ]
  "meta" => array:7 [
    "current_page" => 1
    "from" => 1
    "last_page" => 2
    "path" => "http://localhost/api/users"
    "per_page" => 20
    "to" => 20
    "total" => 31
  ]

Using

    /**
     * Returns the Users resource with the roles relation.
     *
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $paginator = $this->model->with('roles.permissions')->paginate($request->get('limit', config('app.pagination_limit')));
        if ($request->has('limit')) {
            $paginator->appends('limit', $request->get('limit'));
        }

        return UserResource::collection($paginator);
    }

data is empty because I removed it, but the user data is there. I am only worried about like the includes, have not seen how that works.

from laravel-api.

kslimani avatar kslimani commented on June 15, 2024

What happen if instead of /users you rename to something else ?

Route::get('/foobar', function () {
    return new UserCollection(User::paginate());
});

Does it properly generate links like http://localhost/api/foobar?page=1 ?

from laravel-api.

kslimani avatar kslimani commented on June 15, 2024

That's pretty cool !

from laravel-api.

joselfonseca avatar joselfonseca commented on June 15, 2024

Lets leave this for later XD.

from laravel-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.