Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 使用雄辩的ORM从数据库中获取数据_Php_Arrays_Database_Laravel 4_Eloquent - Fatal编程技术网

Php 使用雄辩的ORM从数据库中获取数据

Php 使用雄辩的ORM从数据库中获取数据,php,arrays,database,laravel-4,eloquent,Php,Arrays,Database,Laravel 4,Eloquent,我有一个时间戳数组,我想将它与表中的时间戳进行比较。这是我在控制器中的代码: public function create(){ $from = Input::get('from'); //array of timestamps of current messages displayed $conv_id = Input::get('conv_id'); //array of ids //get all messages related to the array of

我有一个时间戳数组,我想将它与表中的时间戳进行比较。这是我在控制器中的代码:

public function create(){

    $from = Input::get('from'); //array of timestamps of current messages displayed
    $conv_id = Input::get('conv_id'); //array of ids

    //get all messages related to the array of ids
    $allMessages = Messages::whereIn('conv_id',$conv_id);

    //get the messages in database which have timestamps greater than the current ones displayed.
    $messages = $allMessages->where('created_at','>',$from);

    return $messages->get()->reverse();
}
这适用于传递的单个时间戳。但是,如果传递的时间戳数组,则Laravel无法检测到最新消息。我试过了

$messages = $allMessages->whereIn('created_at','>',$from);
但它返回一个数组到字符串的转换错误。我怀疑我可能用错了

因此,我重复一遍,如何获取表中时间戳大于我从
输入
类获取的时间戳的所有消息

我认为这可以使用PHP的
explode()
来完成,但我希望我的代码尽可能地“Laravel ish”。如果
explode()
不可避免,请告诉我如何使用它。顺便说一句,代码改进的额外印象点!谢谢大家!

注意:这是由于
dd($from)


您需要的正是您所拥有的,但是要在数组中找到最新的日期并使用它

这很容易做到

$from = array('2014-06-18 09:10:32', '2014-06-17 10:52:25'); // Input::get('from');
$conv_id = Input::get('conv_id');

asort($from); // Sort the array of dates in order of earliest date first.

$messages = Messages::whereIn('conv_id', $conv_id)->where('created_at', '>', end($from))->get();

这将返回所有创建时间戳大于
$from
数组中最新日期的消息。

如果我误解了您的意思,请原谅,但在我看来,您希望所有消息的时间戳都晚于$from数组中最新的时间戳。如果确实是这样,我认为最佳实践是计算一个时间戳是哪一个,并将其提供给查询-或者,理想情况下,只将最新的日期发送到表单,而不是开始时的数组。我已删除了答案,但这些日期可以使用类似于
asort
的函数进行排序,并且可以轻松获得最大值。@WereWolf TheAlpha感谢您的努力!(非常感谢。)+1,这给我提供了
desc
顺序中的日期,但更好。@Joe是的,当然!为什么我要比较3个时间戳,而所有的时间戳都是按顺序排列的,我可以比较最新的一个?哇,我现在觉得很傻。非常感谢你!很高兴我能帮上忙。作为参考,php.net上的这个页面对于数组排序等非常有用@Joe顺便说一句,end()的反面是什么?我相信沿着这条思路做一些事情是可行的<代码>$first=array\u移位(array\u值($myArray))或使用
$first=reset($array)可能是更好的解决方案。见:
$from = array('2014-06-18 09:10:32', '2014-06-17 10:52:25'); // Input::get('from');
$conv_id = Input::get('conv_id');

asort($from); // Sort the array of dates in order of earliest date first.

$messages = Messages::whereIn('conv_id', $conv_id)->where('created_at', '>', end($from))->get();