วิธีการเชื่อมความสัมพันธ์ข้อมูล one to many บน Laravel Framework
Step 1 - ให้ทำการเปิด Command prompt ขึ้นมา , แล้วใช้คำสั่งสร้าง Model Airport,Airline พร้อมตารางฐานข้อมูลมาด้วยกัน
- ตัวอย่าง Code :
โค้ด: เลือกทั้งหมด
php artisan make:model Airport -m
php artisan make:model Airline -m
- ตัวอย่าง Code :
โค้ด: เลือกทั้งหมด
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAirportsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('airports', function (Blueprint $table) {
$table->integer('id');
$table->string('name');
$table->text('address');
$table->string('image');
$table->decimal('lati',10,6);
$table->decimal('longti',10,6);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('airports');
}
}
- ตัวอย่าง Code :
โค้ด: เลือกทั้งหมด
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAirlinesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('airlines', function (Blueprint $table) {
$table->integer('id');
$table->string('name_airline');
$table->string('name_comp');
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('airlines');
}
}
- ตัวอย่าง คำสั่ง :
โค้ด: เลือกทั้งหมด
php artisan migrate
Step 4 - เราก็จะได้ข้อมูลในตารางของ Airport นี้มา , ในส่วนตารางของ Airline ก็เช่นกัน
Step 5 - ให้เข้าไปที่ตารางฐานข้อมูล airflights
Step 6 - จากนั้นทำการ Update id_airport , id_airline ให้เป็น foreign key Step 7 - ทำการเลือก Relation view
Step 8 - ให้ทำการตั้งชื่อ Constraint name เป็นชื่อเกี่ยวข้อง foreign key เช่น fk_airflight_airline_id , แล้วเปลี่ยนตรงส่วน ON DELETE เป็น NO ACTIONS , แล้วเปลี่ยนตรงส่วน ON UPDATE เป็น CASCADE
Step 9 - ในส่วนตรงนี้ ให้ทำตามตรง ภาพตัวอย่างที่แสดงอยู่
Step 10 - ในส่วนตรงนี้ เชื่อมโยมความสัมพันธ์ ระหว่าง Foreign key(id_airport,id_airline กับ Primary key(id)
Step 11 - เราก็จะได้กุญแจสีเทา ที่เป็น Foreign key มk
Step 12 - ให้ทำการเข้าไปที่ app/Airport
Step 13 - แล้วทำการ ใช้ Function hasMany ในเชื่อมความสัมพันธ์ one to many เชื่อมต่อใน function airflight_1 โดยโยงไปที่ Model Airflight ,โดยมีอ้างอิงกุญแจ foreign key(id_airport) กับ primary key(id) ไป
โค้ด: เลือกทั้งหมด
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Airport extends Model
{
public function airflight_1()
{
return $this->hasMany('App\Airflight','id_airport','id'); //ใช้ Function hasMany ในเชื่อมความสัมพันธ์ one to many เช่น ...
//.... รองเท้ากีฬา -> ประเภท รองเท้า , ประเภท รองเท้า -> รองเท้ากีฬา ....
//.... ประเภท รองเท้า -> รองเท้าส้นสูง ....
//.... ประเภท รองเท้า -> รองเท้าแตะ ....
//.... อันนี้คือ ความสัมพันธ์ แบบ one to many ...
//.... เชื่อมต่อใน function airflight_1 โดยโยงไปที่ Model Airflight ,โดยมีอ้างอิงกุญแจ ...
//.... foreign key(id_airport) กับ primary key(id) ไป
}
}
Step 15 - ใช้ Function hasMany ในเชื่อมความสัมพันธ์ one to many ในส่วนของ Model Airline เชื่อมต่อใน function airflight_1 โดยโยงไปที่ Model Airflight ,โดยมีอ้างอิงกุญแจ foreign key(id_airport) กับ primary key(id) ไป
โค้ด: เลือกทั้งหมด
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Airline extends Model
{
public function airflight_2()
{
return $this->hasMany('App\Airflight','id_airline','id'); //ใช้ Function hasMany ในเชื่อมความสัมพันธ์ one to many ในส่วนของ Model Airline ...
//.... เชื่อมต่อใน function airflight_1 โดยโยงไปที่ Model Airflight ,โดยมีอ้างอิงกุญแจ ...
//.... foreign key(id_airport) กับ primary key(id) ไป
}
}
Step 17 - Model Airflight มีการเชื่อมต่อกับ Model Airport , Model Airline โดยใช้ Function belongsTo โดยมีการอ้างอิงไปที่ foreign key(id_airport) กับ primary key(id) ด้วย
โค้ด: เลือกทั้งหมด
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Airflight extends Model
{
public function airport()
{
return $this->belongsTo('App\Airport', 'id_airport','id'); //Model Airflight มีการเชื่อมต่อกับ Model Airport โดยใช้ Function belongsTo ...
//.... โดยมีการอ้างอิงไปที่ foreign key(id_airport) กับ primary key(id) ด้วย
}
public function airline()
{
return $this->belongsTo('App\Airline', 'id_airline','id');
}
}
ผลลัพธ์ที่ได้
ก็จะมีการแสดง ชื่อที่ตั้งสนามบิน ชื่อสายการบิน ของ ตารางเที่ยวบิน , ที่มีการเชื่อมต่อความสัมพันธ์ฐานข้อมูลในรูปแบบ One to many
ข้อสรุป
Function hasMany เป็นเชื่อมความสัมพันธ์ในรูปแบบของ One to many และมีการใช้ Function belongsTo เข้ามาเชื่อมต่อ ในการทำงานด้วย , ซึ่งจะมีประโยชน์ในเรื่องการจัดการ Database , และมีส่วนในการแสดงการเชื่อมหลายๆตารางในฐานข้อมูล , หากต้องการศึกษาจาก บทเรียน Laravel Framework หลายๆเรื่อง สามารถศึกษาได้จากบทเรียนนี้
แหล่งอ้างอิงข้อมูล
https://laravel.com/docs/8.x/eloquent-relationships
https://seenual.com/laravel-5-4-eloquent-relationships-การกำหนดความสัมพัน/
https://laravel-guide.readthedocs.io/en/latest/eloquent-relationships/