Home

Byphunsanit

Laravel: trait แก้ code ซ้ำ ๆ ทำบ่อย ๆ มาเป็น ตัวเดียวกัน

ของดี อันใหม่ที่มีใน Laravel 11 ถ้าออกแบบ database ไว้โดยคนเดียวกัน จะรู้สึกได้ว่า column มันจะมีความคล้าย ๆ กัน อย่าง column id, enable, title, created_at, updated_at เวลาเขียน code ก็เหมือนกัน อย่างต้องการแสดง option ใน select box ก็จะเขียนออกมาคล้าย ๆ กัน เช่น

    /**
     * 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 โดยวิธีใช้คือ

  1. cd ไปที่ root project
  2. สร้างโดยใช่คำสั่ง รูปแบบ
    php artisan make:trait { class name }
    เช่น
    php artisan make:trait HasOptionsTrait
  3. จะมีไฟล์สร้างขึ้นมาใน App\Traits
  4. แก้ไฟล์ SourceCode/app/Traits/HasOptionsTrait.php คือ copy code ข้างบนมาใส่ใน class นั่นละ
    <?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']);
        }
    }
    
  5. หลังจากนั้นเปิด mode ที่ต้องการใช้ code ชุดนี้ เหมือนมี code ชุดนี้อยู่ในใจ อยู่ในกาย เช่น
    SourceCode/app/Models/Category.php
    <?php
    
    namespace App\Models;
    
    use App\Traits\HasOptionsTrait;
    
    class Category extends Model
    {
        use HasOptionsTrait;
        ...
    }

    อธิบาย
    บรรทัดที่ 5 คือจะดึง code มาจาก App\Traits\HasOptionsTrait
    บรรทัดที่ 9 ใช้ code จาก HasOptionsTrait นะ
  6. test ดูว่าเรียก getOptions() จาก model Category ได้อยู่มั๋ย
  7. เพิ่ม / เปลี่ยนทุก model ที่ใช้ code ชุดนี้ แค่ test ง่าย ๆ ก็พอ ว่ามันทำงานได้ เพราะมันคือ code ชุดเดียวกัน

อ่านเพิ่มเติม