【发布时间】:2021-11-24 17:57:30
【问题描述】:
我想格式化从数据库中获取的某些列,但不知道如何。我使用 Laravel 8.x。这是sn-p:
车库控制器.php
$q = Garage::with('relative')->paginate(15);
return response()->json($q);
输出
{
"data": [
{
"id": 39819,
"name": "john",
"date": "2020-12-20", // i want to format this with date_format()
"relative": {
"rid": 912039,
"rname": "ROV" // and i just want this value instead of all columns
}
},
{
"id": 38178,
"name": "danny",
"date": "2020-12-20", // and this too
"relative": {
"rid": 182738,
"rname": "YIV"
}
}
],
"links": {
.....
},
"meta": {
....
}
}
模型,车库有属于相对的(相对的可以有多个车库)。
public function relative() {
return $this->belongsTo(Relative::class, 'relative', 'rid');
}
我尝试了访问器,但它没有改变任何东西
public function getDateAttributes($value) {
return date_format(date_create($value), 'd/m/Y');
}
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: php laravel eloquent laravel-8 laravel-pagination