0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravelで多対多のテーブルからデータを取得しようとして詰まった話

Posted at

結論:中間テーブルの名前が間違っていた

最初に結論から話しますと、中間テーブルの名前が間違っていました。

マッチングアプリを作成しようとしており、ユーザー情報の入った'users'テーブルとマッチングルームの'rooms'テーブルをまず作成しました。そしてその中間テーブル'room_user'を作成しようとしたのですが、'room_users'になっていました。

原因

なぜ中間テーブル名に余分な's'が入っていたかというと、以下のコマンドでマイグレーションファイルを作成したからです。
php artisan make:model RoomUser -m
これでModelと一緒に'room_users'を作成してしまいました。

解決方法

以下のコマンドでマイグレーションファイルのみを作成して正しいテーブル名にできました。
php artisan make:migration create_room_user_table --create=room_user
タイプミスも見当たらず、リレーションにも間違いが見当たらなかったので、かなり時間を消費しましたが、これで解決することができました。

最後に

かなり初歩的なミスで時間を消費してしまいましたが、同じようなことで詰まっている誰かのお役に立てれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?