【发布时间】:2021-05-17 16:13:31
【问题描述】:
当使用资源 created_at 和 updated_at casting 返回模型时工作正常,
但是当我修改toArray() 函数时,转换不起作用!
在我的模型中:
protected $casts = [
'created_at' => 'datetime:Y-m-d:h',
'updated_at' => 'datetime:Y-m-d:h',
];
在资源中:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'value' => $this->value,
'box' => new BoxResource($this->box),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
在控制器中:
public function index(Request $request)
{
return CurrencyResource::collection(
Currency::where("user_id", "=", $request->user()->id)
->paginate($per_page)
);
}
如何使铸造工作?
【问题讨论】: