lundi 24 août 2015

Laravel 5 relationships with two tables

This question is kind of related to this Laravel 5 get name based on ID

The above questions problem is solved, works perfectly. Now I am trying to do something similar but with more relationships.

So I have a users table

 id | name       | email                | departmentId           
-------------------------------------------------------
 1 | Nick       | nick@email.com       | 2   
-------------------------------------------------------

I also have a clients table

 id | clientName         
-----------------
 18 | McDonalds     
-----------------

So these tables are straight forward. Then I have a projects table

id | projectName     | clientId     | userId             
-------------------------------------------------------
18 | Test Project    | 18           | 1   
-------------------------------------------------------

So this table links to the clients and users tables. A project can have one client and many users. So in Project.php I have

public function client()
{
    return $this->hasOne('App\Client', 'clientId');
}

public function user()
{
    return $this->hasMany('App\user', 'userId');
}

And then in Client.php

public function project()
{
    return $this->belongsTo('App\Project');
}

And User.php

public function project()
{
    return $this->belongsTo('App\Project');
}

So I think the relationships are set up ok. I have some data in the database but when I try to view the index page I get the error.

Column not found: 1054 Unknown column 'clients.clientId' in 'where clause' (SQL: select * from `clients` where `clients`.`clientId` = 2 and `clients`.`clientId` is not null limit 1) 

My mitegations seem to have the correct names

$table->integer('clientId')->unsigned()->default(0);
$table->foreign('clientId')->references('id')->on('clients')->onDelete('cascade');
$table->integer('userId')->unsigned()->default(0);
$table->foreign('userId')->references('id')->on('users')->onDelete('cascade');

Is there anything in the information I have shown which might be causing this error? Or is there anything else I can add?

Thanks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1PuzfvA
via IFTTT

Aucun commentaire:

Enregistrer un commentaire