質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

3347閲覧

3つの外部キーで繋がったテーブルのseedingの書き方が分からない。

HearthXml

総合スコア51

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

1クリップ

投稿2020/04/08 10:20

編集2020/04/09 00:17

概要

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'); } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/04/08 10:34

「DatabaseSeeder.phpでは会社と工場を作成できました」 こう書いているけど、工場を作るSeeder が提示されてませんけど...。
HearthXml

2020/04/08 10:41

追記しました。 ご指摘ありがとうございます。
guest

回答1

0

ベストアンサー

php

1<?php 2 3use Illuminate\Database\Seeder; 4use Illuminate\Support\Facades\DB; 5 6class DatabaseSeeder extends Seeder 7{ 8 /** 9 * Seed the application's database. 10 * 11 * @return void 12 */ 13 public function run() 14 { 15 DB::table('companies')->truncate(); 16 DB::table('factories')->truncate(); 17 DB::table('machines')->truncate(); 18 19 factory(\App\Company::class, 10)->create()->each(function (\App\Company $company){ 20 $company->factories()->save(factory(\App\Factory::class)->make()) 21 ->each(function(\App\Factory $factory){ 22 $factory->machines()->save(factory(Machine::class)->make()); 23 }); 24 }); 25 } 26}

投稿2020/04/09 00:40

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問