dongmei9508 2014-08-06 14:41
浏览 12
已采纳

Laravel透视表n:m关系

I have table organisations and another table 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.

In my model Organisation.php I have the following,

class Organisation extends Eloquent {

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

}

and in my Client.php model I have,

class Client extends Eloquent {

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

}

The Pivot table migration,

<?php

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)

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? I want to be able to get all my organisations and their clients, using the pivot table.

  • 写回答

1条回答 默认 最新

  • dongmu6225 2014-08-06 14:52
    关注

    To use a pivot table, you should use belongsToMany on both ends of the relationship:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?