【发布时间】:2016-04-30 00:04:22
【问题描述】:
我有一个带有索引方法的 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 类的实例。
【问题讨论】:
-
尝试从您的查询中删除
->get() -
@smartrahat。我这样做了,但随后它返回了 QueryBuilder 对象的实例并返回了此错误:
Argument 1 passed to Dingo\Api\Http\Response\Factory::collection() must be an instance of Illuminate\Support\Collection, instance of Illuminate\Database\Query\Builder given, called in C:\xampp\htdocs\escape\app\Http\Controllers\ArticlesCommentsController.php on line 41 and defined
标签: php laravel eloquent dingo-api