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

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

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

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

解決済

1回答

1127閲覧

LaravelのModelのリレーション

Chandler_Bing

総合スコア673

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

0クリップ

投稿2019/06/10 07:08

create_people_table

php

1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreatePeopleTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('people', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->string('name'); 19 $table->string('mail'); 20 $table->integer('age'); 21 $table->timestamps(); 22 }); 23 } 24 25 /** 26 * Reverse the migrations. 27 * 28 * @return void 29 */ 30 public function down() 31 { 32 Schema::dropIfExists('people'); 33 } 34} 35

Personモデル

php

1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6use Illuminate\Database\Eloquent\Builder; 7use App\Scopes\ScopePerson; 8 9class Person extends Model 10{ 11 protected $guarded = array('id'); 12 13 public static $rules = array( 14 'name' => 'required', 15 'mail' => 'email', 16 'age' => 'integer|min:0|max:150' 17 ); 18 19 public function getData() 20 { 21 return $this->id . ': ' . $this->name . ' (' . $this->age . ')'; 22 } 23 24 public function boards() 25 { 26 return $this->hasMany('App\Board'); 27 } 28}

create_boards_table

php

1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateBoardsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('boards', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->integer('person_id'); 19 $table->string('title'); 20 $table->string('message'); 21 $table->timestamps(); 22 }); 23 } 24 25 /** 26 * Reverse the migrations. 27 * 28 * @return void 29 */ 30 public function down() 31 { 32 Schema::dropIfExists('boards'); 33 } 34}

Boardモデル

php

1<?php 2namespace App; 3 4use Illuminate\Database\Eloquent\Model; 5 6class Board extends Model 7{ 8 protected $guarded = array('id'); 9 10 public static $rules = array( 11 'person_id' => 'required', 12 'title' => 'required', 13 'message' => 'required' 14 ); 15 16 // 新たにメソッドを追加 17 public function person() 18 { 19 return $this->belongsTo('App\Person'); 20 } 21 22 // 既にあるメソッドを修正 23 public function getData() 24 { 25 return $this->id . ': ' . $this->title . ' (' 26 . $this->person->name . ')'; 27 } 28} 29

疑問
これはPersonモデルを主テーブルとして扱っているようです(参考書に従っています)
疑問なのが、外部キー制約を全く記述していないのに、peopleテーブルのidとboardsテーブルのperson_idが紐づけされています。

これはboardsテーブルにpeopleの単数形であるpersonとidをくっつけたperson_idカラムがあるので、Laravelが自動的に判断しているのですか。

今まで触った参考書や学習サイトでは必ず外部キー制約をテーブルにつけていました。

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

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

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

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

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

m.ts10806

2019/06/10 07:31

疑問ではなく質問をしてください。
guest

回答1

0

ベストアンサー

belongsToメソッドで、第二引数を指定せずに、自動で外部キー名が取得されているのは何故かということであれば、

return $this->belongsTo('App\Person');

belongsToメソッドの中で、第2引数がない場合は、以下のロジックでキー名を自動生成しています。

if (is_null($foreignKey)) { $foreignKey = Str::snake($relation).'_'.$instance->getKeyName(); }

投稿2019/06/10 08:16

aro10

総合スコア4106

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問