【问题标题】:Laravel 4.2 Many-to-many relationship : Can't read from pivot tableLaravel 4.2 多对多关系:无法从数据透视表中读取
【发布时间】:2014-08-05 20:10:41
【问题描述】:

我试图将 Jeffrey Way 的多对多关系教程应用到我的私人消息应用程序中,但我卡住了。我正在尝试进行 2 个对话,hahahehe 与用户相关联。但是,Laravel 给了我错误:

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.我试过了: 在控制器中:

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

对话return $this->hasMany('Conversations','conv_id');

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

2。我也试过:

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

对话return $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');
    }
}

控制器

对话控制器

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

迁移(在函数 up() 中)

用户

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

改进代码的奖励积分。非常感谢!

【问题讨论】:

  • 如果您尝试实现的是多对多关系,那么您的 User 和 Conversations 模型都应该是 belongsToMany。
  • @Jeemusu 是的,你是对的。我正要回答我的问题,因为我最近在再次查看 Mr. Way 的教程时才发现它。也许您可以回答它并进一步解释为什么 hasMany-belongsToMany 不能建立多对多关系?谢谢!

标签: php mysql laravel laravel-4 many-to-many


【解决方案1】:

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

在这种情况下,我们需要对用户模型上的关系和对话模型上的逆向关系使用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
);

【讨论】:

  • 谢谢!顺便说一句,我可以知道为什么 hasMany-belongsToMany 不起作用吗?听起来是正确的,不是吗,User hasMany conversations 和一个 conversations belongsToMany Users?
  • hasMany中,多个foo可能只绑定到一个bar,但在belongsToMany中,绑定到bar的foo也可能绑定到另一个,假设baz。使用hasMany 不适合那里。一个用户可能有多个对话,一个对话可能在多个用户之间。
【解决方案2】:

也许它会帮助某人。我浪费了很多时间,然后查看数据库...

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

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2018-09-15
    • 2023-02-05
    • 2018-09-23
    • 1970-01-01
    • 2015-02-10
    相关资源
    最近更新 更多