目前我在 laravel 中有這個雄辯的選擇$queryBooking = Booking::where('id',$id)->select('starts_at','ends_at')->first();現在我想將特定格式放入starts_at并ends_at使用這種格式Y-m-d數據應該像這樣顯示2020-05-29 (Y-m-d)我怎樣才能使用 laravel 雄辯地做到這一點?更新嘗試了 spiny 的答案做了一些修改。 $queryBooking = Booking::where('id',$id)->select('starts_at','ends_at')->first(); $booking = PropertyBooking::where('property_id',$queryBooking->property_id) ->selectRaw( 'DATE_FORMAT(`starts_at`, "%Y-%m-%d") AS `starts_at`, DATE_FORMAT(`ends_at`, "%Y-%m-%d") AS `ends_at`' )->get(); return view('admin.pages.property_request.edit', compact('property_request','booking'));在我的刀片中嘗試使用 js 記錄它,就像這樣
2 回答

慕森王
TA貢獻1777條經驗 獲得超3個贊
這個怎么樣:
->selectRaw(
'DATE_FORMAT(`starts_at`, "%Y-%m-%d") AS `starts_at`, DATE_FORMAT(`ends_at`, "%Y-%m-%d") AS `ends_at`'
)
您也可以將其用作范圍:
public function scopeSelectStartEndDates($query) {
return $query->selectRaw(
'DATE_FORMAT(`starts_at`, "%Y-%m-%d") AS `starts_at`, DATE_FORMAT(`ends_at`, "%Y-%m-%d") AS `ends_at`'
);
}
$model = Model::selectStartEndDates()->first();

阿晨1998
TA貢獻2037條經驗 獲得超6個贊
只需在您的預訂中添加一些屬性。日期屬性將列轉換為碳。dateFormat 屬性是將日期轉換為字符串的默認格式
例子
class Booking extends Model
{
protected $dates = ['starts_at','ends_at'];
protected $dateFormat = 'Y-m-d';
....
}
- 2 回答
- 0 關注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消