Laravel 4.2 Many-to-many relationship : Can't read from pivot table

Mark

I was trying to apply Jeffrey Way's many-to-many relationships tutorial into my private messaging app but I got stuck. I'm trying to get 2 conversations, haha and hehe which are associated to a user. However, Laravel gives me the error :

Column not found: 1054 Unknown column 'Conversations.user_id' in 'where clause' (SQL: select * from `Conversations` where `Conversations`.`user_id` = 1)

I have these data in my conversations table :

+---------+----------+
| conv_id | name     |
+---------+----------+
|       1 |     haha |
|       2 |     hehe |
+---------+----------+

In my user_conversations table :

+----+-------------+--------+
| id | conv_id     | user_id|
+----+-------------+--------+
|  1 |           1 |      1 |
|  2 |           2 |      1 |
+----+-------------+--------+

1. I've tried : In controllers :

User : return $this->belongsToMany('User','id');

Conversatons : return $this->hasMany('Conversations','conv_id');

but the result I got was : haha instead of haha and hehe

2. I've also tried :

User : return $this->belongsToMany('User','user_conversations');

Conversatons : return $this->hasMany('Conversations','user_conversations');

but laravel returned me the following error :

Column not found: 1054 Unknown column 'Conversations.user_conversations' in 'where clause' (SQL: select * from `Conversations` where `Conversations`.`user_conversations` = 1)

I'm still relatively new in Laravel so I might be making some silly mistakes.


here are my codes :

MODEL

Conversations

class Conversations extends Eloquent  {

    protected $table = 'Conversations';

    protected $fillable = array('name');

    public function users(){
        return $this->belongsToMany('User');
    }

}

User

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    ....

    public function conversations(){
        return $this->hasMany('Conversations');
    }
}

CONTROLLERS

Conversations Controller

public function create()
    {
        $loginuser = User::find(Auth::user()->id);
        $conversations = $loginuser->conversations;
        return View::make('msgsystem.Conversations.Conversations',array('conversations'=>$conversations));
    }

MIGRATIONS (in function up())

user

Schema::create('users',function($table)
        {
            $table->increments('id');
            $table->string('email')->unique();
            $table->string('password',100);
            $table->string('name',150);
            $table->string('usertype',50);
            $table->boolean('block');
            $table->string('remember_token',100);
            $table->timestamp('lastlogin_at');
            $table->timestamps();
            $table->softDeletes();
        });

conversations

Schema::create('Conversations', function(Blueprint $table)
        {
            $table->increments('conv_id')->index();
            $table->string('name',100);
            $table->timestamps();
        });

user_conversations

Schema::create('user_conversations', function(Blueprint $table)
        {
            $table->increments('id')->unsigned();
            $table->integer('conversation_id')->unsigned()->index();
            $table->foreign('conversation_id')->references('conv_id')->on('conversations')->onDelete('cascade');
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });

Bonus points for improvement of code. Thank you very much!

Jeemusu

In your example of a many to many relationship a user can have many conversations, where the conversations can also be shared by many users.

In this case we need to use the belongsToMany() method for both the relationship on the user model, and the inverse on the conversations model.

class User 
{
    public function conversations()
    {
        return $this->belongsToMany('Conversations');
    }
}



class Conversations 
{
    public function users()
    {
        return $this->belongsToMany('User');
    }
}

If you need to use a different name for your pivot table, or override the associated keys, you can pass them in as optional arguments.

return $this->belongsToMany(
   'Conversations',          // related_table
   'user_conversations',     // pivot_table
   'user_id',                // key_1
   'conversations_id'        // key_2
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel 4 - Many to Many - pivot table not updating

From Dev

Can't show elements from many to many relationship Laravel 5.2

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

From Dev

Laravel - Order by pivot value in Many to Many table relationship

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Unable to sync conditional pivot table many to many relationship laravel

From Dev

Laravel L5.5 No access to pivot table in "Many to many" relationship

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many

From Dev

In Laravel 4 Using creating a many to many relationship between 2 databases

From Dev

Laravel Many-to-Many Pivot Table

From Dev

rails 4 many to many through pivot table

From Dev

Eloquent many to many relationship access table column value from pivot table

From Dev

Eager loading with many to many relationship in laravel 4

From Dev

Has many through with pivot table in Laravel4

From Dev

Need to set a 1 to many relationship in the same table in Laravel 4

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

Can't create many-to-many relationship

From Dev

Laravel many to many relationship, add more objects into "pivot"?

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Laravel 5.2 has many relationship - can't access fields

From Dev

I can not get values from table. Relationship one to many

From Dev

Pivot Table with many to many table

From Dev

Laravel 4 how to set up many-to-many relationship with the same table

Related Related

  1. 1

    Laravel 4 - Many to Many - pivot table not updating

  2. 2

    Can't show elements from many to many relationship Laravel 5.2

  3. 3

    Laravel Eloquent Many to Many relationship using a pivot table

  4. 4

    Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

  5. 5

    Laravel - Order by pivot value in Many to Many table relationship

  6. 6

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  7. 7

    Unable to sync conditional pivot table many to many relationship laravel

  8. 8

    Laravel L5.5 No access to pivot table in "Many to many" relationship

  9. 9

    Laravel Removing Pivot data in many to many relationship

  10. 10

    Creating one to many relationship with pivot table of many to many relationship

  11. 11

    Creating one to many relationship with pivot table of many to many relationship

  12. 12

    laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many

  13. 13

    In Laravel 4 Using creating a many to many relationship between 2 databases

  14. 14

    Laravel Many-to-Many Pivot Table

  15. 15

    rails 4 many to many through pivot table

  16. 16

    Eloquent many to many relationship access table column value from pivot table

  17. 17

    Eager loading with many to many relationship in laravel 4

  18. 18

    Has many through with pivot table in Laravel4

  19. 19

    Need to set a 1 to many relationship in the same table in Laravel 4

  20. 20

    How to query a pivot table data in MySQL (many to many relationship)

  21. 21

    Can't create many-to-many relationship

  22. 22

    Laravel many to many relationship, add more objects into "pivot"?

  23. 23

    Laravel - Many to Many relationship

  24. 24

    Many to many relationship in Laravel

  25. 25

    Many to Many relationship with Laravel

  26. 26

    Laravel 5.2 has many relationship - can't access fields

  27. 27

    I can not get values from table. Relationship one to many

  28. 28

    Pivot Table with many to many table

  29. 29

    Laravel 4 how to set up many-to-many relationship with the same table

HotTag

Archive