Laravel 4多对多关系不工作,无法找到数据表 [英] Laravel 4 many to many relationship not working, pivot table not found
问题描述
模型是:
<?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');
}
}
以下是按照标准填充数据透视表的方式。当然,还有land,objs和land_objs表:
<?php
use Illuminate\ Database\Migrations\Migration;
class CreateLandsObjsTable extends Migration {
/ **
*运行迁移。
*
* @return void
* /
public function up()
{
Schema :: create('lands_objs',function ){
$ table-> integer('land_id');
$ table-> integer('obj_id');
});
}
}
有了这个结构,我应该能够感谢雄辩:
$ land = Land :: find(1); // checked loads land fine
$ objs = $ land-> objs; // - >在这里我收到错误
但我收到错误:
SQLSTATE [42S02]:未找到基表或视图:1146表'taylor.land_obj'不存在
(SQL:select`objs`。* ```````````````````````````````````````````````````````````` `obj_id`
其中`land_obj`.`land_id` =?)(Bindings:array(0 => 1,))
尽管在land_obj上进行了查询,不应该Laravel创建表land_objs?我错过了什么吗?
非常感谢。
枢轴表应该是链接的表名称的单数版本,按字母顺序,所以在您的情况下:
land_obj而不是land_objs
如果您不想使用默认的命名约定,您还可以在模型中的belongsToMany调用中将表名称指定为第二个参数:
return $ this-> belongsToMany('Obj','lands_objs');
和
return $ this-> belongsToMany('Land','lands_objs');
有关更多信息,请参阅这里的文档
I'm currently having problems with a n:n relationship with Laravel 4, I'm having an error wiht pivot table which is quering on a table with with both components on singular name. I create a pivot table lands_objs and populate it:
Models are:
<?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');
}
}
Here is how I populate the pivot table following the standards. Of course lands, objs and lands_objs tables exist:
<?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');
});
}
}
With this structure I should be able to do thanks to Eloquent:
$land = Land::find(1); //checked loads land fine
$objs = $land->objs; //--> HERE I TAKE THE ERROR
But 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, ))
Shouldn't Laravel create the table lands_objs in spite of quering on land_obj? Am I missing something?
Thanks a lot.
The pivot table should be singular version of the table names it is linking, in alphabetical order so in your case:
land_obj rather than lands_objs
If you really don't want to use the default naming conventions you can also specify the table name as the 2nd param on the belongsToMany call in your models:
return $this->belongsToMany('Obj', 'lands_objs');
and
return $this->belongsToMany('Land', 'lands_objs');
For more information see docs here
这篇关于Laravel 4多对多关系不工作,无法找到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!