Sample Database
Id scholar_birthday
1 2001-12-05
2 2002-10-15
3 1991-12-19
4 1990-02-25
我在从数据库列 scholar_birthday 获取数据时遇到问题。我想要的是单独获取年,月和日,以便我可以将它们作为参数传递给 createFromFormat()。
$input = Scholar::select('scholar_birthday')->get();
$strFromArr = serialize($input);
$format = 'Y/m/d';
$year = 'Y';
$month = 'm';
$day = 'd';
dd($format);
$date = Carbon::createFromFormat($year,$month,$day, $strFromArr);
dd($date);
但结果(来自 dd()的返回值)是:
无法找到四位数的年份
有没有人知道如何以正确的方式实现这个?
答案 0 :(得分:0)
最好在mysql查询中处理。
SELECT YEAR(scholar_birthday) as year,
MONTH(scholar_birthday) as month,
DAY(scholar_birthday) as day
FROM Scholar
现在我对laravel模型并不完全熟悉,但看起来你可以这样做......
$fields = "YEAR(scholar_birthday) as year, MONTH(scholar_birthday) as month, DAY(scholar_birthday) as day";
$input = Scholar::select($fields)->get();