Mar*_*ark 3 php mysql many-to-many laravel laravel-4
我试图将Jeffrey Way的多对多关系教程应用到我的私人消息应用程序中,但我被卡住了.我正在尝试进行2次对话,haha并且hehe与用户相关联.但是,Laravel给了我错误:
Column not found: 1054 Unknown column 'Conversations.user_id' in 'where clause' (SQL: select * from `Conversations` where `Conversations`.`user_id` = 1)
Run Code Online (Sandbox Code Playgroud)
我在对话表中有这些数据:
+---------+----------+
| conv_id | name |
+---------+----------+
| 1 | haha |
| 2 | hehe |
+---------+----------+
Run Code Online (Sandbox Code Playgroud)
在我的user_conversations表中:
+----+-------------+--------+
| id | conv_id | user_id|
+----+-------------+--------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
+----+-------------+--------+
Run Code Online (Sandbox Code Playgroud)
我试过:在控制器中:
用户:return $this->belongsToMany('User','id');
对话:return $this->hasMany('Conversations','conv_id');
但我得到的结果是:haha而不是haha和hehe
我也尝试过:
用户: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)
Run Code Online (Sandbox Code Playgroud)
我在Laravel还是比较新的,所以我可能会犯一些愚蠢的错误.
这是我的代码:
模型
对话
class Conversations extends Eloquent {
protected $table = 'Conversations';
protected $fillable = array('name');
public function users(){
return $this->belongsToMany('User');
}
}
Run Code Online (Sandbox Code Playgroud)
用户
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
....
public function conversations(){
return $this->hasMany('Conversations');
}
}
Run Code Online (Sandbox Code Playgroud)
CONTROLLERS
对话控制器
public function create()
{
$loginuser = User::find(Auth::user()->id);
$conversations = $loginuser->conversations;
return View::make('msgsystem.Conversations.Conversations',array('conversations'=>$conversations));
}
Run Code Online (Sandbox Code Playgroud)
移民(在功能上升())
用户
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();
});
Run Code Online (Sandbox Code Playgroud)
对话
Schema::create('Conversations', function(Blueprint $table)
{
$table->increments('conv_id')->index();
$table->string('name',100);
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
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();
});
Run Code Online (Sandbox Code Playgroud)
改善代码的奖励点.非常感谢你!
在您的多对多关系的示例中,用户可以进行多次对话,其中对话也可以由许多用户共享.
在这种情况下,我们需要将该belongsToMany()方法用于用户模型上的关系和对话模型上的逆.
class User
{
public function conversations()
{
return $this->belongsToMany('Conversations');
}
}
class Conversations
{
public function users()
{
return $this->belongsToMany('User');
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要为数据透视表使用不同的名称,或覆盖关联的键,则可以将它们作为可选参数传递.
return $this->belongsToMany(
'Conversations', // related_table
'user_conversations', // pivot_table
'user_id', // key_1
'conversations_id' // key_2
);
Run Code Online (Sandbox Code Playgroud)