2014-06-16 113 views
3

我试图将Jeffrey Way的多对多关系教程应用到我的私人消息应用程序中,但是我被卡住了。我试图获得与用户关联的2个对话,hahahehe。然而,Laravel给我的错误:Laravel 4.2多对多的关系:无法从数据透视表中读取

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

我有我的谈话表中的这些数据:

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

在我user_conversations表:

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

1.我已经试过: In controllers:

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

Conversatonsreturn $this->hasMany('Conversations','conv_id');

,但我得到的结果是:haha而不是hahahehe

2.我也试过:

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

Conversatonsreturn $this->hasMany('Conversations','user_conversations');

但laravel回到我下面的错误:

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

我仍然在Laravel相对较新,所以我可能会犯一些愚蠢的错误。


这里是我的代码:

模型

对话

class Conversations extends Eloquent { 

    protected $table = 'Conversations'; 

    protected $fillable = array('name'); 

    public function users(){ 
     return $this->belongsToMany('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

会话控制器

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

MIGRATIONS(在功能上())

用户

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(); 
     }); 

对话

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(); 
     }); 

奖励积分。非常感谢你!

+2

如果您尝试实施的是多对多关系,则您的用户和对话模型应该是belongsToMany。 – Jeemusu

+0

@Jeemusu是的,你说得对。我正要回答我的问题,因为我最近发现了这个问题,当我再次查看Way先生的教程时。也许你可以回答它,并进一步解释为什么hasMany-belongsToMany不具有多对多的关系?谢谢! – Mark

回答

6

在您的多对多关系示例中,用户可以进行多次对话,其中对话也可以由多个用户共享。

在这种情况下,我们需要将belongsToMany()方法用于用户模型的关系和对话模型的逆。

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



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

如果需要使用不同的名称为您的数据透视表,或覆盖相关的键,可以作为可选参数传递它们。

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

谢谢!顺便说一句,我可以知道为什么许多 - belongsToMany不起作用?这听起来是正确的,是不是,用户有很多的谈话和谈话belongsToMany用户? – Mark

+1

在'hasMany'中,多于一个'foo'可以绑定到一个'bar',但在'belongsToMany'中,绑定到'bar'的foo也可以绑定到另一个,比如'baz'。使用'hasMany'并不适合那里。用户可能有多个对话,并且对话可能在多个用户之间。 – Arda

0

也许它会帮助别人。我失去了许多小时,然后看数据库...

我忘了设置'deleted_at'默认值为NULL(当使用软删除)。

相关问题