Laravel 4 多对多关系不起作用,找不到数据透视表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16384926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:45:26  来源:igfitidea点击:

Laravel 4 many to many relationship not working, pivot table not found

modellaravelmany-to-manylaravel-4eloquent

提问by Joss

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:

我目前在与 Laravel 4 的 an:n 关系方面遇到问题,我在使用数据透视表时出现错误,该数据透视表在两个组件均具有单数名称的表上查询。我创建了一个数据透视表lands_objs并填充它:

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:

这是我按照标准填充数据透视表的方法。当然,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');
        });
    }
}

With this structure I should be able to do thanks to Eloquent:

有了这个结构,我应该能够做到多亏 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?

尽管在land_obj上查询,Laravel不应该创建表lands_objs吗?我错过了什么吗?

Thanks a lot.

非常感谢。

回答by Brent

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

land_obj 而不是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:

如果您真的不想使用默认命名约定,您还可以在模型中的belongsToMany 调用中将表名指定为第二个参数:

return $this->belongsToMany('Obj', 'lands_objs');

and

return $this->belongsToMany('Land', 'lands_objs');

For more information see docs here

有关更多信息,请参阅此处的文档