0

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,

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.

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
Cycs
  • 229
  • 1
  • 5
  • 15

1 Answers1

2

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.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Ah excellent that gets rid of the error, but returns `Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( ) )` I was expecting an object of organisations and clients – Cycs Aug 06 '14 at 14:58
  • The reason is that the class names are used. `Client` and `Organisation` in the relations. – peter.babic Aug 06 '14 at 14:59
  • 1
    @Cycs - Exactly what code are you writing and what are you expecting instead? – Joseph Silber Aug 06 '14 at 15:00
  • What should I use then? The table names? – Cycs Aug 06 '14 at 15:00
  • @JosephSilber I am expecting an object containing my organisations and nested in each organisation another object container it's clients – Cycs Aug 06 '14 at 15:02
  • 1
    @Cycs - That's exactly what you got: a collection of your objects. What else do you want? – Joseph Silber Aug 06 '14 at 15:04
  • Then why is it seemingly empty, I have 1 organisation, and they has 1 client. Maybe I am confused, should I not be able to do, `$organisations = new Organisation; $org = $organisations->projects()->get()` and then die(print_r($org)) and see an object? not `Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( ) )` – Cycs Aug 06 '14 at 15:09
  • 1
    @Cycs - How do you intend to load relationships to an entry that doesn't exist? – Joseph Silber Aug 06 '14 at 15:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58800/discussion-between-cycs-and-joseph-silber). – Cycs Aug 06 '14 at 15:16
  • I would assume it would return null values for organisations that have no clients? – Cycs Aug 06 '14 at 15:18
  • @Cycs Read this http://stackoverflow.com/questions/23910553/laravel-check-if-related-model-exists#answer-23911985 – Jarek Tkaczyk Aug 06 '14 at 17:39
  • Looks like this is a misunderstanding. Try `$org = $organisations->projects()->get()->toArray()` and then dump that. It should show you the array. Dumping the collection won't actually show all the items in the collection. – user1669496 Aug 06 '14 at 18:06