Laravel : Dingo/API Pagination custom root key

2019-09-14 17:52发布

问题:

I've developed an API with Laravel 5 and Dingo/API.

Following the documentation, i used pagination and my code look like that

$users = User::paginate(50);

return $this->response->paginator($users, new UserTransformer);

Unfortunately, the response root key is "data"

{
"data": [
{
  "id": 1,
  "username": "superuser", 
......

I'd like to change the "data" key to a custom one, because in my case, emberjs get this response and try to make a link with a "datum" model which doesn't exist, the key need to be set with the same name as the ember model in case of a RESTAdapter.

I already tried some parameters in the response but nothing change

return $this->response->paginator($users, new UserTransformer, ['key' => 'users']);

or

return $this->response->paginator($users, new UserTransformer, ['identifier' => 'users']);

Nothing work, i'm stuck with the "data" key.

Is someone have a solution ?

Thank you in advance.

回答1:

I managed to fix my problem.

I don't modify the api.php configuration, transformer stay the same

'transformer' => env('API_TRANSFORMER', Dingo\Api\Transformer\Adapter\Fractal::class),

Firstly i create a new serializer

// app/Api/V1/Serializers/CustomJsonSerializer.php

<?php namespace App\Api\V1\Serializers;

use League\Fractal\Pagination\CursorInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Serializer\SerializerAbstract;


/**
 * Create a new Serializer in your project
 */
use League\Fractal\Serializer\ArraySerializer;

class CustomJsonSerializer extends ArraySerializer
{
    public function collection($resourceKey, array $data)
    {
        if ($resourceKey === false) {
            return $data;
        }
        return array($resourceKey ?: 'data' => $data);
    }

    public function item($resourceKey, array $data)
    {
        if ($resourceKey === false) {
            return $data;
        }
        return array($resourceKey ?: 'data' => $data);
    }
}

And i set my new custom serializer inside the AppServiceProviders

// app\Providers\AppServiceProviders.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Dingo\Api\Transformer\Adapter\Fractal;
use League\Fractal\Manager;
use App\Api\V1\Serializers\CustomJsonSerializer;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
             $fractal = new Manager();
             $fractal->setSerializer(new CustomJsonSerializer());
             return new Fractal($fractal);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

I hope it'll help ppl :)



回答2:

or you can use something like this

Answer::where('question_id', '=', $questionId)
  ->join('articles', 'answers.article_id', '=', 'articles.id')
  ->orderBy('count_thanks', 'desc')
  ->limit($perPage)
  ->offset($offset)
  ->get();