概要
Laravelで外部キー制約のあるテストデータを作ろうと思いましたが、
会社テーブルと工場テーブル、工場テーブルと機械テーブルが外部キーで繋がっている場合の簡潔な書き方がわかりません。
DatabaseSeeder.php
では会社と工場を作成できましたが、機械データは無理やり作る形でしか実装できませんでした。
開発環境
Laravel6
postgres11
DatabaseSeeder.php
<?php use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { DB::table('companies')->truncate(); DB::table('factories')->truncate(); DB::table('machines')->truncate(); factory(\App\Company::class, 10)->create()->each(function ($company){ $company->factories()->save(factory(\App\Factory::class)->make()); } Factory::all()->each(function ($factory){ $factory->machines()->save(factory(Machine::class)->make()); }); }
Model
Company.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; /** * Class Company * @package App */ class Company extends Model { protected $fillable = [ 'id', 'name' ]; public function factories() { return $this->hasMany('App\Factory'); } public function user() { return $this->hasMany('App\User'); } }
Factory.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; class Factory extends Model { public $incrementing = false; protected $keyType = 'string'; protected static function boot() { parent::boot(); static::creating(function ($model) { $model->{$model->getKeyName()} = Str::uuid(); }); } public function company(){ return $this->belongsTo('App\Company'); } public function machines(){ return $this->hasMany('App\Machine'); } }
Machine.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; class Machine extends Model { public $incrementing = false; protected $keyType = 'string'; protected static function boot() { parent::boot(); static::creating(function ($model) { $model->{$model->getKeyName()} = Str::uuid(); }); } public function factory() { return $this->belongsTo('App\Factory'); } public function machinePerformances() { return $this->hasMany('App\MachinePerformance'); } }
Factory
CompanyFactory.php
<?php /** @var \Illuminate\Database\Eloquent\Factory $factory */ use Faker\Generator as Faker; use App\Company; $factory->define(Company::class, function (Faker $faker) { return [ 'name' => $faker->company, ]; });
FactoryFactory.php
<?php /** @var \Illuminate\Database\Eloquent\Factory $factory */ use App\Model; use Faker\Generator as Faker; $factory->define(\App\Factory::class, function (Faker $faker) { return [ 'name' => $faker->name, ]; });
MachineFactory.php
<?php /** @var \Illuminate\Database\Eloquent\Factory $factory */ use Faker\Generator as Faker; $factory->define(\App\Machine::class, function (Faker $faker) { return [ 'name' => $faker->name, 'threshold' => $faker->randomFloat(1), 'device_id' => 'AAA' ]; });
マイグレーションファイル
companyテーブル
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateCompaniesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('companies', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('companies'); } }
factoryテーブル
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateFactoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('factories', function (Blueprint $table) { $table->uuid('id'); $table->string('name'); $table->bigInteger('company_id')->unsigned(); $table->timestamps(); $table->primary('id'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('factories'); } }
Machineテーブル
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateMachinesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('machines', function (Blueprint $table) { $table->uuid('id'); $table->string('name'); $table->double('threshold')->nullable(); $table->uuid('factory_id')->unsigned(); $table->string('device_id')->unique(); $table->timestamps(); $table->foreign('factory_id') ->references('id') ->on('factories') ->onDelete('cascade'); $table->primary('id'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('machines'); } }
回答1件
あなたの回答
tips
プレビュー