Laravel รองรับการสร้างคอลัมน์วันเวลาใน migration ได้หลายรูปแบบ เช่น dateTime(), timestamp() เป็นต้น โดยใช้ dateTime ซึ่งใน SQL Server จะถูก map เป็น datetime (แบบเดิม)
เมื่อใช้ Laravel ร่วมกับฐานข้อมูล Microsoft SQL Server โดยเฉพาะเวอร์ชัน 2008 ขึ้นไป อาจจะพบปัญหาเรื่องการจัดเก็บข้อมูลวันเวลา โดยเฉพาะ Laravel ส่งค่าที่มีจุดทศนิยมหลายหลัก ซึ่งอาจทำให้เกิด error เช่น “The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.” ดังนั้นเมื่อต้องใช้งานร่วมกับ SQL Server 2008 ซึ่งมีข้อจำกัดบางประการ การเลือกชนิดข้อมูลและความละเอียด (precision) ของเวลาให้เหมาะสมจึงเป็นสิ่งสำคัญ
การกำหนดชนิดข้อมูลวันที่เวลาใน Laravel Migration
Laravel จะใช้เมธอด dateTime() ใน migration เพื่อสร้างฟิลด์วันเวลา
เช่น
Schema::create('events', function (Blueprint $table) {
$table->id();
$table->dateTime('scheduled_at'); // ค่า default คือ precision = 0
$table->timestamps(); // สร้าง created_at และ updated_at
});
ถ้าต้องการกำหนดความละเอียดของเวลา (precision) เพิ่ม จะเขียนแบบนี้
$table->dateTime('scheduled_at', 3); // datetime2(3)
$table->timestamps(3); //created_at , updated_at มีค่า precision = 3
Laravel จะ map ให้เป็น datetime2 ตามความละเอียดที่กำหนด (เฉพาะกับ SQL Server)
วิธีการเรียกใช้
- ไม่ต้องการเก็บค่าจุดทศนิยม
ไฟล์ migration
Schema::create('tests', function (Blueprint $table) {
$table->id();
$table->dateTime('custom');
$table->timestamps();
//หรือใช้
//$table->dateTime('created_at');
//$table->dateTime('updated_at');
//แทน $table->timestamps();
});
จากนั้นทำการ Migration โดยใช้ php artisan serve
ไฟล์ model
protected $casts = [
'custom' => 'datetime', //มีค่า 'd/m/y H:i:s'
];
protected $dateFormat = 'd/m/y H:i:s'; //จุดทศนิยม 0 ตัว เฉพาะ created_at, updated_at
ไฟล์ Controller
$dt = \Carbon\Carbon::now();
$test = new Test();
$test->custom = $dt;
$test->created_at = $dt;
$test->updated_at = $dt;
$test->save();

หรือ อีกวิธีแบบหนึ่ง ไม่แสดงจุดทศนิยม
ไฟล์ Migration
Schema::create('tests', function (Blueprint $table) {
$table->id();
$table->dateTime('custom');
$table->timestamps();
});
DB::statement("ALTER TABLE tests DROP COLUMN custom");
DB::statement("ALTER TABLE tests ADD custom datetime2(0) NOT NULL");
DB::statement("ALTER TABLE tests DROP COLUMN created_at");
DB::statement("ALTER TABLE tests ADD created_at datetime2(0) NOT NULL");
DB::statement("ALTER TABLE tests DROP COLUMN updated_at");
DB::statement("ALTER TABLE tests ADD updated_at datetime2(0) NOT NULL");
จากนั้นทำการ Migration โดยใช้ php artisan serve
ไฟล์ Controller
$dt = \Carbon\Carbon::now();
$test = new Test();
$test->custom = $dt;
$test->created_at = $dt;
$test->updated_at = $dt;
$test->save();

2. ถ้าต้องการเก็บค่าจุดทศนิยม 3 ตำแหน่ง ดังตัวอย่าง
ไฟล์ Migration
Schema::create('tests', function (Blueprint $table) {
$table->id();
$table->dateTime('custom', 3);
$table->timestamps(3);
});
จากนั้นทำการ Migration โดยใช้ php artisan serve
ไฟล์ Controller
$dt = \Carbon\Carbon::now();
$test = new Test();
$test->custom = $dt
$test->created_at = $dt;
$test->updated_at = $dt;
$test->save();

สรุปคำแนะนำ
– ใช้ dateTime() พร้อม precision เช่น (3) เพื่อความเข้ากันได้กับ SQL Server 2008
– ให้ตรวจสอบว่าเวลาที่ส่งค่าจาก Laravel ไม่เกินความละเอียดที่ SQL Server รองรับ
– หากต้องการใช้ created_at และ updated_at หรือ timestamps() ควรกำหนด precision ด้วย เช่น timestamps(3)
การสร้างฟิลด์วันเวลาให้ถูกต้องและสอดคล้องกับ SQL Server 2008 เป็นสิ่งสำคัญเมื่อต้องพัฒนาแอปพลิเคชัน Laravel กับระบบฐานข้อมูล โดยการกำหนด precision ที่เหมาะสม และจัดรูปแบบชนิดข้อมูลจากฝั่ง Laravel จะช่วยลดความผิดพลาดและทำให้ระบบทำงานได้อย่างราบรื่น