Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 4.2:如何使用where()和with()来获取一对多关系中的项目列表?_Php_Mysql_Laravel_Laravel 4_One To Many - Fatal编程技术网

Php Laravel 4.2:如何使用where()和with()来获取一对多关系中的项目列表?

Php Laravel 4.2:如何使用where()和with()来获取一对多关系中的项目列表?,php,mysql,laravel,laravel-4,one-to-many,Php,Mysql,Laravel,Laravel 4,One To Many,我想尝试根据会话ID显示消息。规则是一个会话有多条消息和一条消息只属于一个会话。 我尝试过的 我的控制器中有: public function index() { $from = Input::get('from',null); //timestamp of latest message $conv_id = Input::get('conv_id'); //get an array of conversation ids $allmes

我想尝试根据会话ID显示消息。规则是一个会话有多条消息一条消息只属于一个会话。

我尝试过的

我的控制器中有:

public function index()
    {
        $from = Input::get('from',null); //timestamp of latest message
        $conv_id = Input::get('conv_id'); //get an array of conversation ids

        $allmessages = Conversations::whereIn('id',$conv_id)->with('messages');
        if(is_null($from)){
           $messages = $allmessages->orderBy('created_at','desc')->take(10);
        }else{
           $messages = $allmessages->where('created_at','>',$from);
        }
        return $messages->get()->reverse();
    }
但我收到了错误信息

"SQLSTATE[42S22]:Column not found: 1054 Unknown column 'messages.conversations_id' in 
'where clause'(SQL: select * from `messages` where `messages`.`conversations_id` in (1, 2))",
我似乎想不出哪里出了错。代码的改进将是一个奖励。谢谢大家!


我有两种模式,对话和信息。这些是桌子。出于这个问题的目的,我故意省略了两个表的时间戳列

对话

+---------+----------+
|      id | name     |
+---------+----------+
|       1 |     haha |
|       2 |     hehe |
+---------+----------+
+---------+----------+-----------------+
| user_id | conv_id  |body             |
+---------+----------+-----------------+
|       1 |     1    |user1 says hi!   |
|       2 |     1    |user2 says seen! |
+---------+----------+-----------------+
消息

+---------+----------+
|      id | name     |
+---------+----------+
|       1 |     haha |
|       2 |     hehe |
+---------+----------+
+---------+----------+-----------------+
| user_id | conv_id  |body             |
+---------+----------+-----------------+
|       1 |     1    |user1 says hi!   |
|       2 |     1    |user2 says seen! |
+---------+----------+-----------------+
下面是链接这两个模型的函数

对话模式

public function messages(){
        return $this->hasMany('Messages');
    }
 public function conversations(){
        return $this->belongsTo('Conversations');
    }
消息模型

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

你的错误信息是

messages.conversations\u id

但是您的表模式列为

conv_id


因此,您似乎在某处使用了错误的id字段(我无法告诉您在哪里,因为您没有发布足够的代码-但您应该能够找到它。如果没有,请从您的模型中发布更多代码)。

您的错误消息说

messages.conversations\u id

但是您的表模式列为

conv_id


因此,您似乎在某处使用了错误的id字段(我无法告诉您在哪里,因为您没有发布足够的代码-但您应该能够找到它。如果没有,请从您的模型中发布更多代码)。

您的错误消息说

messages.conversations\u id

但是您的表模式列为

conv_id


因此,您似乎在某处使用了错误的id字段(我无法告诉您在哪里,因为您没有发布足够的代码-但您应该能够找到它。如果没有,请从您的模型中发布更多代码)。

您的错误消息说

messages.conversations\u id

但是您的表模式列为

conv_id


因此,您似乎在某处使用了错误的id字段(我无法告诉您在哪里,因为您没有发布足够的代码,但您应该能够找到它。如果没有,请从您的模型中发布更多代码)。

尝试将您的消息模型中的关系更改为:

public function conversations() {
    return $this->belongsTo('Conversations', 'conv_id');
}

第二个参数指定要用作外键的列:默认情况下,它将是“conversations\u id”(基于模型名称)。

尝试将消息模型中的关系更改为:

public function conversations() {
    return $this->belongsTo('Conversations', 'conv_id');
}

第二个参数指定要用作外键的列:默认情况下,它将是“conversations\u id”(基于模型名称)。

尝试将消息模型中的关系更改为:

public function conversations() {
    return $this->belongsTo('Conversations', 'conv_id');
}

第二个参数指定要用作外键的列:默认情况下,它将是“conversations\u id”(基于模型名称)。

尝试将消息模型中的关系更改为:

public function conversations() {
    return $this->belongsTo('Conversations', 'conv_id');
}
第二个参数指定要用作外键的列:默认情况下,它将是“conversations_id”(基于模型名称)