Skip to main content

เนื้อหาภาษาไทย / Thai content

System guide Content reviewed

การสร้างความสัมพันธ์ระหว่าง Model (Many to Many)

ความสัมพันธ์แบบ Many to Many ใน Laravel คือการเชื่อมโยงระหว่างสองตารางที่แต่ละแถวในตารางหนึ่งสามารถเชื่อมโยงกับหลายแถวในอีกตารางหนึ่ง และในทางกลับกัน ตัวอย่างเช...

การสร้างความสัมพันธ์แบบ Many to Many

ใน Laravel การสร้างความสัมพันธ์แบบ Many to Many จะใช้เมธอด belongsToMany() ใน Model โดยทั่วไปจะมีการใช้ตารางกลาง (Pivot Table) เพื่อเก็บข้อมูลความสัมพันธ์ระหว่างทั้งสองตาราง

ตัวอย่างการสร้างความสัมพันธ์ Many to Many ระหว่าง Model User และ Role

สมมติว่าเรามีตาราง users และ roles ซึ่งมีความสัมพันธ์กันแบบหลายต่อหลาย โดยมีตารางกลาง role_user ที่ใช้เก็บความสัมพันธ์ระหว่างผู้ใช้และบทบาท

  1. สร้าง Model และ Migration สำหรับ User และ Role
php artisan make:model User -m
php artisan make:model Role -m

2. สร้าง Migration สำหรับตารางกลาง

สร้างตาราง role_user สำหรับเก็บข้อมูลความสัมพันธ์ระหว่างผู้ใช้และบทบาท โดยไม่จำเป็นต้องมี Primary Key เนื่องจากใช้เพื่อเก็บความสัมพันธ์เท่านั้น

php artisan make:migration create_role_user_table

3. กำหนดโครงสร้างตารางในไฟล์ Migration

ในไฟล์ create_users_table.php:

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamps();
});

ในไฟล์ create_roles_table.php:

Schema::create('roles', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});

ในไฟล์ create_role_user_table.php (ตารางกลาง):

Schema::create('role_user', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('role_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});

4. กำหนดความสัมพันธ์ใน Model

ใน Model User ให้เพิ่มฟังก์ชัน roles() ที่ใช้เมธอด belongsToMany() เพื่อเชื่อมโยงกับ Role

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

ใน Model Role ให้เพิ่มฟังก์ชัน users() ที่ใช้เมธอด belongsToMany() เพื่อเชื่อมโยงกลับไปที่ User

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}

การใช้งานความสัมพันธ์แบบ Many to Many

หลังจากสร้างความสัมพันธ์แล้ว เราสามารถเรียกใช้ข้อมูลจากความสัมพันธ์นี้ได้โดยตรง เช่น

use App\Models\User;
use App\Models\Role;

// ดึงบทบาททั้งหมดของผู้ใช้ที่มี ID เท่ากับ 1
$user = User::find(1);
$roles = $user->roles; // ดึงข้อมูล Role ทั้งหมดของ User นี้

// ดึงข้อมูลผู้ใช้ทั้งหมดที่มีบทบาทที่กำหนด
$role = Role::find(1);
$users = $role->users; // ดึงข้อมูล User ที่มี Role นี้

การบันทึกข้อมูลในความสัมพันธ์แบบ Many to Many

เราสามารถใช้เมธอด attach() และ detach() เพื่อบันทึกข้อมูลความสัมพันธ์หรือยกเลิกความสัมพันธ์ได้ และใช้ sync() เพื่อจัดการความสัมพันธ์ทั้งหมดในครั้งเดียว

// เพิ่มบทบาทให้ผู้ใช้
$user = User::find(1);
$user->roles()->attach(2); // เพิ่ม Role ที่มี ID = 2 ให้กับ User ที่มี ID = 1

// ยกเลิกบทบาทของผู้ใช้
$user->roles()->detach(2); // ยกเลิก Role ที่มี ID = 2 สำหรับ User นี้

// อัปเดตบทบาททั้งหมด
$user->roles()->sync([1, 3]); // ตั้งค่า Role ที่มี ID 1 และ 3 ให้กับ User โดยยกเลิก Role ที่ไม่อยู่ในรายการนี้

การนำไปใช้งาน

การสร้างความสัมพันธ์ Many to Many ใน Laravel ช่วยให้การจัดการข้อมูลที่มีความสัมพันธ์กันระหว่างสองตารางที่เชื่อมโยงหลายต่อหลายเป็นไปได้อย่างง่ายดายและสะดวก ทำให้การดึงและอัปเดตข้อมูลความสัมพันธ์มีความเป็นระบบ และสามารถจัดการข้อมูลได้อย่างรวดเร็วด้วยคำสั่งของ Eloquent

Office.in.th Custom Service

Want to build or adapt a system from this structure?

Office.in.th designs and develops enterprise web applications, API integrations, and public- and private-sector management systems, with long-term support.

See our software development service