[Laravel] Relations Factory หากต้องการสร้าง data จำลองสำหรับทดสอบแบบที่มีความสัมพันธ์กันคงจะดีไม่น้อย

Image placeholder
แวะมาทักทายกันได้

[Laravel] Relations Factory หากต้องการสร้าง data จำลองสำหรับทดสอบ

จากบทความที่ผ่านมา [Laravel] Data Factory คลิ๊กเดียวชีวิตง่ายขึ้น 


จะเห็นว่าถ้าอยากจะสร้างข้อมูลจำลองขึ้นมา แค่สร้าง factory และ seeder ขึ้นมาช่วยให้สะดวกในการพัฒนามากยิ่งขึ้น 

แต่เป็นเพียงแค่สร้างข้อมูลจำลองเพียงแค่ตารางเดียว 


ฝากกดโฆษณา Google Ads สัก click  เพื่อเป็นกำลังใจแก่ผู้เขียนด้วยนะครับ


คำถามคือ ถ้าอยากสร้างข้อมูลจำลองในหลายๆตารางที่มีความสัมพันธ์ทำอย่างไร


ในบทความนี้ก็เป็นอีกหนึ่งตัวอย่างที่ผู้เขียนได้ลองทำการทดสอบ จากความเข้าใจที่ไปอ่าน document ของ laravel มาในเรื่องของ Model Relations


สำหรับบทความนี้จะนำ ความสัมพันธ์แบบ One To One มาทำการทดลอง


เริ่มจากขั้นตอนที่

1. ให้ทำต่อจากบทความที่แล้ว php artisan make:migration create_income.

สำหรับสร้างไฟล์สำหรับ create table database ขึ้นมา

$table->unsignedBigInteger('id_coupon')->index()->nullable();

สำหรับ ทำ relation กับ table ของ coupon ที่สร้างไว้ในบทความที่แล้ว

$table->foreign('id_coupon')->references('id')->on('coupons')->onDelete('cascade');

สำหรับสร้าง Relation


class CreateIncome extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('incomes', function (Blueprint $table) {
$table->id();
$table->string('income');

                    // foreign key need to unsignedBigInteger
$table->unsignedBigInteger('id_coupon')->index()
->nullable();

// id of relation table, name not must same
                    $table->foreign('id_coupon')->references('id')
->on('coupons')->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('incomes');
}
}


2. สร้างไฟล์ Factory สำหรับ Income Class

php artisan make:factory IncomeFactory --model=Income แล้วทำการใส่ค่าจำลอง

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Income;
use Faker\Generator as Faker;

$factory->define(Income::class, function (Faker $faker) {
return [
'income' => random_int(10000, 50000)
];
});


3. สร้างไฟล์ model

php artisan make:model Income

class Income extends Model {}


4. ไปที่ ไฟล์ Model Coupon ที่สร้างเอาไว้ในบทความที่แล้ว แก้ไขไฟล์ตามนี้ 

hasone เป็นการสร้างความสัมพันธ์แบบ 1:1 ระหว่าง table Coupon และ Income

class Coupon extends Model
{
/**
* Get the user associated with the Income
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function income()
{
return $this->hasOne(Income::class, 'id_coupon', 'id');
}
}


5. สร้างไฟล์ Seeder สำหรับ Income 

php artisan make:seeder IncomeSeeder

class IncomeSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Coupon::class, 50)->create();
}
}


6. เมื่อทำเสร็จทั้งหมดแล้วก็บรรเลง สร้าง Data ได้จากคำสั่ง 

php artisan db:seed --class=CouponSeeder


ถ้าไม่มีอะไรผิดพลาดก็จะได้ตามภาพ

table coupon



table Income



 จะเห็นว่า id_coupon จะเป็น Foriegn Key ของตารางโดยที่ เชื่อมกับ Id ของ ตาราง Coupon



แวะมาทักทายกันได้
donate

Categories: Tutorial Tags: #programing , #laravel , 1190