0
我試着變換對象像爲:分形變壓器Laravel?
$objects = Object::all();
$objects = (new ObjectTransformer)->transform($objects);
其中ObjectTransformer
是:
class ObjectTransformer extends TransformerAbstract {
public function transform(Object $obj)
{
return [
'id' => (int) $obj->id,
'name' => $obj->prefix
];
}
}
我得到一個錯誤:
Type error: Argument 1 passed to App\Http\Controllers\API\ObjectTransformer::transform() must be an instance of App\Http\Controllers\API\Object, instance of Illuminate\Database\Eloquent\Collection given
你能解釋一下我當我有object:'$ obj-> prototypez() - > get() - > first()時,如何在'Fractal \ Resource \ Item'中使用Transformer' – Gagamerov
我的意思是嵌套關係 – Gagamerov
不,我只是回答你的題。 :) – Amarnasan