ของดี อันใหม่ที่มีใน Laravel 11 ถ้าออกแบบ database ไว้โดยคนเดียวกัน จะรู้สึกได้ว่า column มันจะมีความคล้าย ๆ กัน อย่าง column id, enable, title, created_at, updated_at เวลาเขียน code ก็เหมือนกัน อย่างต้องการแสดง option ใน select box ก็จะเขียนออกมาคล้าย ๆ กัน เช่น
1 2 3 4 5 6 7 8 9 10 11 | /** * Get the options for the select input * * @return \Illuminate\Database\Eloquent\Collection */ public static function getOptions() { return self::where( 'enable' , true) ->orderBy( 'title' ) ->get([ 'title as text' , 'id as value' ]); } |
อันนี้ ถ้ามี 6 model ที่โครงสร้างตารางเหมือนกัน แล้วต้องการเอาไปแสดงเหมือนกัน จะบอกว่าเขียนมา 1 ที่ แล้วก็ copy แล้วไป paste อีก จริง ๆ มันก็ไม่ผิดหรอก แต่ถ้าหากเงื่อนไข มันซับซ้อนขึ้น หรือเจอจุดผิดขึ้นมาในต้นฉบับ แปลว่าตัวที่ copy ไป คือต้องไล่แก้ ไล่ test ใหม่หมดเลย มันจะดีกว่าถ้าเปลี่ยนมาเชียนแบบ traits
Laravel 11 มาพร้อมของใหม่ คือ php artisan make:trait
โดยวิธีใช้คือ
- cd ไปที่ root project
- สร้างโดยใช่คำสั่ง รูปแบบ
php artisan make:trait { class name }
เช่นphp artisan make:trait HasOptionsTrait
- จะมีไฟล์สร้างขึ้นมาใน App\Traits
- แก้ไฟล์ SourceCode/app/Traits/HasOptionsTrait.php
คือ copy code ข้างบนมาใส่ใน class นั่นละ
1234567891011121314151617
<?php
namespace
App\Traits;
trait
HasOptionsTrait
{
/**
* Get the options for the select input
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public
static
function
getOptions()
{
return
self::where(
'enable'
, true)
->orderBy(
'title'
)
->get([
'title as text'
,
'id as value'
]);
}
}
- หลังจากนั้นเปิด mode ที่ต้องการใช้ code ชุดนี้ เหมือนมี code ชุดนี้อยู่ในใจ อยู่ในกาย เช่น
SourceCode/app/Models/Category.php1234567891011<?php
namespace
App\Models;
use
App\Traits\HasOptionsTrait;
class
Category
extends
Model
{
use
HasOptionsTrait;
...
}
อธิบาย
บรรทัดที่ 5 คือจะดึง code มาจาก App\Traits\HasOptionsTrait
บรรทัดที่ 9 ใช้ code จาก HasOptionsTrait นะ - test ดูว่าเรียก getOptions() จาก model Category ได้อยู่มั๋ย
- เพิ่ม / เปลี่ยนทุก model ที่ใช้ code ชุดนี้ แค่ test ง่าย ๆ ก็พอ ว่ามันทำงานได้ เพราะมันคือ code ชุดเดียวกัน
อ่านเพิ่มเติม