简体   繁体   English

Laravel 数据透视表 n:m 关系

[英]Laravel pivot tables n:m relationships

I have table organisations and another table clients .我有表organisations和另一个表clients An organisation can have many clients, and a client can belong to many organisations hence the many-to-many relationship and the pivot table client_organisation .一个组织可以有多个客户端,一个客户端可以属于多个组织,因此存在多对多关系和数据透视表client_organisation

In my model Organisation.php I have the following,在我的模型Organisation.php我有以下内容,

class Organisation extends Eloquent {

    //Organisation __has_many__ clients
    public function clients()
    {
        return $this->hasMany('client');
    }

}

and in my Client.php model I have,在我的Client.php模型中,

class Client extends Eloquent {

    public function organisations()
    {
        return $this->belongsToMany('organisation');
    }

}

The Pivot table migration,数据透视表迁移,

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateClientOrganisationTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('client_organisation', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('client_id')->unsigned()->index();
            $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
            $table->integer('organisation_id')->unsigned()->index();
            $table->foreign('organisation_id')->references('id')->on('organisations')->onDelete('cascade');
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('client_organisation');
    }

}

I then run the following in my controller, to retrieve all the organisations and there clients,然后我在我的控制器中运行以下命令,以检索所有组织和客户端,

$organisations = new Organisation;  
$organisations->clients()->get();

however this results in the following error,但这会导致以下错误,

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'clients.organisation_id' in 'where clause' (SQL: select * from clients where clients . organisation_id is null) SQLSTATE[42S22]:未找到列:1054 未知列“clients.organisation_id”在“where 子句”中(SQL:从clients中选择 *,其中clients organisation_id为空)

Now it is my understanding that should not need a clients.organisation_id column in my database as I have a pivot table, what am I doing wrong?现在我的理解是我的数据库中不需要clients.organisation_id列,因为我有一个数据透视表,我做错了什么? I want to be able to get all my organisations and their clients, using the pivot table.我希望能够使用数据透视表获取我所有的组织及其客户。

To use a pivot table, you should use belongsToMany on both ends of the relationship:要使用数据透视表,您应该在关系的两端使用belongsToMany

class Organisation extends Eloquent {

    public function clients()
    {
        return $this->belongsToMany('Client');
    }

}

class Client extends Eloquent {

    public function organisations()
    {
        return $this->belongsToMany('Organisation');
    }

}

Note that the first argument to belongsToMany is the name of the class, which is capitalized.请注意, belongsToMany的第一个参数是类的名称,大写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM