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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器