2016-12-08 5 views
0
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

어쩌면 당신은 자동 scholar_birthday을 추가하여, 탄소 객체에 값을 구문 분석 할 수 ... 나는 laravel 모델과 완전히 익숙하지 않은,하지만 당신이 할 수있는 것 같습니다 모델의 private $ dates 속성 (https://laravel.com/docs/5.3/eloquent-mutators#date-mutators) – mirko911

+0

@ mikro911 - 실제로 내 프로젝트에서 laravel 5.0을 사용하고 있습니다. – Gatzmar

답변

0

이것은 mysql 쿼리에서 가장 잘 처리됩니다. 이제

SELECT YEAR(scholar_birthday) as year, 
     MONTH(scholar_birthday) as month, 
     DAY(scholar_birthday) as day 
FROM Scholar 

$fields = "YEAR(scholar_birthday) as year, MONTH(scholar_birthday) as month, DAY(scholar_birthday) as day"; 
$input = Scholar::select($fields)->get();