使用带有laravel 5.1的dingo / APi + Fractal /变换器的laravel原始查询

时间:2016-01-23 11:22:14

标签: php laravel eloquent dingo-api

我有一个带有索引方法的ArticleCommentsController

class ArticleCommentsController extends BaseController
{
    public function index($id)
    {

        $comments = DB::table('comments')
            ->leftJoin('users', 'users.id', '=', 'comments.user_id')
            ->where('comments.article_id', '=', $id)
            ->get();

         return $this->response->item($comments, new CommentTransformer);
    }
}

这是变压器类

namespace App\Transformers;

use League\Fractal\TransformerAbstract;

class CommentTransformer extends TransformerAbstract{
    public function transform($comment)
    {
        return $comment; //simplified
    }
}

响应是以下错误:

get_class() expects parameter 1 to be object, array given.

显然,我需要在调用Fractal \ transform时发送一个comment对象的实例,但我不知道怎么做,因为laravel的raw查询只返回一个数组或QueryBuilder类的一个实例。

3 个答案:

答案 0 :(得分:2)

可悲的是,item对象上的response方法似乎需要和对象而不是数组。使用array方法可行,但不会使用您传递的任何变换器。

所以,我认为您可能会使用ArrayObject离开,如下所示:

return $this->response->item(new ArrayObject($comments), new CommentTransformer);

请记得在文件顶部放置一个use ArrayObject;

答案 1 :(得分:1)

这是很久以前的事了,但如果我失去记忆,我会在将来为这个人或其他人或我写下答案哈哈哈

class ArticleCommentsController extends BaseController
{
    public function index($id)
    {

        $comments = DB::table('comments')
            ->leftJoin('users', 'users.id', '=', 'comments.user_id')
            ->where('comments.article_id', '=', $id)
            ->get();

         return $this->response->collection(Collection::make($comments), new CommentTransformer);

    }
}

当然你需要将它添加到控制器ArticleCommentsController

// Dingo
use Dingo\Api\Routing\Helpers;

//Convert query to collective
use Illuminate\Support\Collection;

//Transformers for API
use App\Transformers\CommentTransformer;

并且在你的功能之前在你的控制器里面

//Use for Dingo Helpers
use Helpers;

所有在一起:

<?php

namespace App\Http\Controllers;
use Response;
use App\User;
use App\Http\Requests;
use Illuminate\Http\Request;

// Dingo
use Dingo\Api\Routing\Helpers;

//Convert query from LMS lbrary to collective
use Illuminate\Support\Collection;

//Transformers for API
use App\Transformers\CommentTransformer;

class ArticleCommentsController extends BaseController
{

    //Use for Dingo Helpers
    use Helpers;

    public function index($id)
    {

        $comments = DB::table('comments')
            ->leftJoin('users', 'users.id', '=', 'comments.user_id')
            ->where('comments.article_id', '=', $id)
            ->get();

         return $this->response->collection(Collection::make($comments), new CommentTransformer);

    }
}

关心!,我希望将来对其他人有所帮助:D

答案 2 :(得分:1)

执行以下步骤:

1.更改

return $this->response->item($comments, new CommentTransformer);

return $this->response->collection(Collection::make($comments), new CommentTransformer);

2.Transfomer class

namespace App\Transformers;
use League\Fractal\TransformerAbstract;

class CommentTransformer extends TransformerAbstract{
     public function transform($comment)
     {
         return [
            'id' => $comment->id,
            ...
         ];
     }
}