4

我在我的 Laravel 项目中使用了 Transformer。当我在 Transformer 中不包含其他对象时没有任何问题,但是当我包含该Customer对象时,我收到以下错误:

传递给 App\Transformers\CustomerTransformer::transform() 的参数 1 必须是 App\Models\Customer 的实例,给定布尔值,在 /home/vagrant/Code/project/vendor/league/fractal/src/Scope.php 中调用在第 365 行并定义

当我从那里打印对象时,Scope.php其中没有任何布尔值。可能是什么问题呢?(代码在 Review 之后崩溃#298

在此处输入图像描述

我如何调用代码:

$reviews = $this->review->paginate();
$transformer = new ReviewTransformer();
$with = $request->get('with', null);
if($with) {
    $with = explode(';', $with);
    $transformer->parseIncludes($with);
}
return $this->response->paginator($reviews, $transformer); 
4

1 回答 1

8

解决了这个问题,我是个白痴。。

我的 Transformer 类中包含以下内容:

public function includeCustomer(Review $review) 
{
    $customer = $review->customer;
    return $this->collection($customer, new CustomerTransformer);
}

问题是这$customer是一个项目而不是一个集合。我不得不更改this->collectionthis->item.

于 2016-09-20T08:00:51.817 回答