1

我有一个带有索引方法的 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 时发送评论对象的实例,但我不知道该怎么做,因为 laravel 的原始查询只返回数组或 QueryBuilder 类的实例。

4

3 回答 3

2

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

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

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

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

于 2016-01-23T16:56:19.817 回答
1

执行以下步骤,它的工作原理:

1.改变

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

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

2.变压器类

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

class CommentTransformer extends TransformerAbstract{
     public function transform($comment)
     {
         return [
            'id' => $comment->id,
            ...
         ];
     }
}
于 2017-05-05T06:54:56.493 回答
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

于 2016-09-25T02:04:45.530 回答