我有一个带有索引方法的 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 类的实例。