当使用资源返回模型 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)
);
}
如何使铸造工作?