私は現在、Laravel 4 との an:n 関係に問題があります。単数形の名前の両方のコンポーネントを持つテーブルでクエリを実行しているピボット テーブルでエラーが発生しています。ピボット テーブル lands_objs を作成し、データを入力します。
モデルは次のとおりです。
<?php
class Obj extends Eloquent
{
protected $guarded = array();
public static $rules = array();
public $timestamps = false;
public function land()
{
return $this->belongsToMany('Land');
}
<?php
class Land extends Eloquent
{
protected $guarded = array();
public static $rules = array();
public $timestamps = false;
public function objs()
{
return $this->belongsToMany('Obj');
}
}
標準に従ってピボット テーブルにデータを入力する方法を次に示します。もちろん、lands、objs、lands_objs テーブルが存在します。
<?php
use Illuminate\Database\Migrations\Migration;
class CreateLandsObjsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('lands_objs', function($table) {
$table->integer('land_id');
$table->integer('obj_id');
});
}
}
この構造では、Eloquent のおかげでできるはずです。
$land = Land::find(1); //checked loads land fine
$objs = $land->objs; //--> HERE I TAKE THE ERROR
しかし、私はエラーを取ります:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'taylor.land_obj' doesn't exist
(SQL: select `objs`.*, `land_obj`.`land_id` as `pivot_land_id`, `land_obj`.`obj_id`
as `pivot_obj_id` from `objs` inner join `land_obj` on `objs`.`id` = `land_obj`.`obj_id`
where `land_obj`.`land_id` = ?) (Bindings: array ( 0 => 1, ))
land_obj のクエリにもかかわらず、Laravel はテーブル lands_objs を作成すべきではありませんか? 何か不足していますか?
どうもありがとう。