Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel with SQL server 2008 throw "Conversion of a varchar data type to a datetime data type resulted in an out-of-range value"

I am using laravel 4.1 with SQL Server 2008

I created a model based on Eloquent orm with timestamp:

class Attendance extends \Eloquent {

protected $table = 'Attendance';

public function users(){return $this->belongsToMany('User', 'Users_Attendance', 'user_id', 'attendance_id');}

}

When I try to insert new fields:

public function postAttendanceUsers() {
    $attendance = new Attendance;
    $attendance->user_id = Auth::user()->id;
    $attendance->save();
}

Display the following error:

SQLSTATE[22007]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server] Conversion of a varchar data type to a datetime data type resulted in an out-of-range value. (SQL: insert into [Attendance] ([user_id], [updated_at], [created_at]) values (40, 2015-08-27 12:28:42.000, 2015-08-27 12:28:42.000))

like image 635
jics Avatar asked Aug 27 '15 15:08

jics


1 Answers

I created a new method with the following format:

 /**
 * Get the format for database stored dates.
 *
 * @return string
 */
public function getDateFormat() {
    return 'Y-d-m H:i:s';
}

I hope somebody be useful

like image 196
jics Avatar answered Nov 07 '22 01:11

jics