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

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

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

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

Q&A

解決済

1回答

1689閲覧

Laravel Eloquentのリレーションのid名

退会済みユーザー

退会済みユーザー

総合スコア0

Laravel

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

0グッド

0クリップ

投稿2020/06/30 11:29

編集2020/07/01 07:30

疑問点

Laravelでfactoryを使ってseedingをしようと試みているのですが、
下記のようなエラーが発生してしまいました。
(このプログラムでは、usersテーブルとschedulesテーブルを利用しています。)

Unknown column 'schedule_scheduleId'とあるように、model名+primary keyの名前のprimary keyを生成しているようにみえます。

この命名のルールのようなものを変更することはできますか?
それとも、このルールに従って、usersテーブルのscheduleのid名を付けるべきでしょうか?

vagrant@homestead:~/code/scheduler$ php artisan db:seed Seeding: ScheduleSeeder Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'schedule_scheduleId' in 'field list' (SQL: insert into `users` (`userName`, `schedule_scheduleId`) values (Keegan, 1)) at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671 667| // If an exception occurs when attempting to run a query, we'll format the error 668| // message to include the bindings with SQL, which will make this exception a 669| // lot more helpful to the developer instead of just the database's errors. 670| catch (Exception $e) { > 671| throw new QueryException( 672| $query, $this->prepareBindings($bindings), $e 673| ); 674| } 675| • A column was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`. https://laravel.com/docs/master/migrations#running-migrations +13 vendor frames 14 database/seeds/ScheduleSeeder.php:16 Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() +8 vendor frames 23 database/seeds/DatabaseSeeder.php:15 Illuminate\Database\Seeder::call()

app/Schedule.php

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Schedule extends Model { protected $primaryKey = 'scheduleId'; public $timestamps = false; // public function users() { return $this->hasMany('App\User'); } }

2020_06_30_000013_create_schedules_table.php

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateSchedulesTable extends Migration { public function up() { Schema::create('schedules', function (Blueprint $table) { $table->increments('scheduleId'); $table->string('scheduleName'); $table->string('scheduleUuid'); }); } public function down() { Schema::dropIfExists('schedules'); } }

2020_06_29_235942_create_users_table.php

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('userId'); $table->string('userName'); $table->integer('scheduleId'); }); } public function down() { Schema::dropIfExists('users'); } }

ScheduleSeeder.php

<?php use Illuminate\Database\Seeder; class ScheduleSeeder extends Seeder { public function run() { // factory(App\Schedule::class)->create()->users()->save(factory(App\User::class)->make()); } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

リレーション部分の書き方が違います。

ドキュメント引用

hasOneメソッドと同様に、外部キーとローカルキーをhasManyメソッドに追加の引数として渡すことでオーバーライドできます。

php

1return $this->hasMany('App\Comment', 'foreign_key'); 2 3return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

第二、第三引数をせっていしなかった場合、モデル名と主キーをもとに自動で外部キー名が生成されます。

投稿2020/07/01 01:32

編集2020/07/01 01:33
hayato7

総合スコア1135

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

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

退会済みユーザー

退会済みユーザー

2020/07/01 07:27

回答ありがとうございます。 Factoryではなく、そもそもEloquentのリレーションを理解できてなかったためですね。 この度はありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問