###前提・実現したいこと
laravelで口コミ付きのランキングサイトを作っています。
ranking modelの各項目が複数のreputation modelを持つ仕様です
** ranking:1 reputation:N のhasMany
###発生している問題・エラーメッセージ
外部キー制約をつけた上で、seedデータを投入した際に、
rankingモデルが持つはずのreputations()メソッドがundefinedとなりseedに失敗します
Seeding: RankingsTableSeeder Seeding: ReputationsTableSeeder [BadMethodCallException] Call to undefined method Illuminate\Database\Query\Builder::reputations()
下記に、各モデルとmigrationデータ、seedファイルを記載しましたが
個人的にはしっかりメソッドを定義してる認識なのですが、どこか間違いがありますでしょうか・・・?
ご教示いただけると助かります。
###該当のソースコード
Reputationモデル
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Reputation extends Model { protected $guarded = []; public function ranking() { return $this->belongsTo('App\Ranking'); } }
Rankingモデル
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Ranking extends Model { protected $guarded = []; public function reputations() { return $this->hasMany('App\Reputation'); } }
migration (一部カラムのみ抜粋)
class CreateRankingsTable extends Migration { public function up() { Schema::create('rankings', function (Blueprint $table) { $table->increments('id'); $table->string('service_id',255)->unique(); $table->string('service_type',255); $table->integer('rank'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('rankings'); } } class CreateReputationsTable extends Migration { public function up() { Schema::create('reputations', function (Blueprint $table) { $table->increments('id'); $table->integer('age'); $table->string('gender',50); $table->integer('rating'); $table->text('comment'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('reputations'); } } // 外部キー制約追加用マイグレーション class AddColumnToReputationsTable extends Migration { public function up() { Schema::table('reputations', function (Blueprint $table) { $table->integer('rankings_id')->unsigned(); $table->foreign('rankings_id')->references('id')->on('rankings')->onDelete('cascade'); }); } public function down() { Schema::table('reputations', function (Blueprint $table) { $table->dropForeign('reputations_rankings_id_foreign'); $table->dropColumn('rankings_id'); }); } }
シードデータ
class RankingsTableSeeder extends Seeder { public function run() { DB::table('rankings')->delete(); $data = [ [ 'service_id' => 'hoge1', 'service_type' => 'fuga1', 'rank' => 1, ], [ 'service_id' => 'hoge2', 'service_type' => 'fuga2', 'rank' => 2, ], [ 'service_id' => 'hoge3', 'service_type' => 'fuga3', 'rank' => 3, ], [ 'service_id' => 'hoge4', 'service_type' => 'fuga4', 'rank' => 4, ], ]; for($i = 0; $i < count($data); $i++){ Ranking::create([ 'service_id' => $data[$i]['service_id'], 'service_type' => $data[$i]['service_type'], 'rank' => $data[$i]['rank'], ]); } } } class ReputationsTableSeeder extends Seeder { public function run() { DB::table('reputations')->delete(); $count = App\Ranking::all()->count(); for($i = 0; $i < $count; $i++){ for($k = 0; $k < 10; $k++){ $rep = new App\Reputation([ 'age' => $ages[mt_rand(0,count($ages) - 1)], 'gender' => $genders[$i % count($genders)], 'rating' => mt_rand(0,5), 'comment' => $comments[mt_rand(0,count($comments) - 1)], ]); } // ここでreputationsがundefinedとなるようです $ranking = App\Ranking::all()->orderBy('rank','asc')->take(1)->skip($i)->reputations()->save($rep); } } }
###試したこと
スペルミスも、単数複数もすべて試しましたがダメでした。
また補足情報として、mariadbのtableのprefixとして"tj_"というstringがつくのですが
このprefixがもしや悪さをしているかと思い、
Reputationモデル
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Reputation extends Model { protected $guarded = []; public function ranking() { return $this->belongsTo('App\Ranking','tj_rankings_id'); } }
Rankingモデル
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Ranking extends Model { protected $guarded = []; public function reputations() { return $this->hasMany('App\Reputation','tj_rankings_id','id); } }
のように試しましたがそれでも同様のエラーでした。
###補足情報(言語/FW/ツール等のバージョンなど)
FW:laravel5.3
DB:mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
Centos7.3
お手数おかけしますが、どなたかご教示ください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/17 16:38