1

I have an array of timestamps and I want to compare it with the ones in a table. This is my code in my controller :

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

This works for a single timestamp passed. However, if it was an array of timestamps that was passed, Laravel was not able to detect the latest messages. I have tried,

$messages = $allMessages->whereIn('created_at','>',$from);

But it returns an Array To String conversion error. I suspect I might be using it wrongly.

So I repeat, how do I get all messages in the table which have timestamps greater than the timestamps that I got from the Input class?

I think this can be done using PHP's explode() but I want to keep my codes as 'Laravel-ish' as possible. If explode() is unavoidable, please show me how to use it. Btw, bonus impression points for improvement of code! Thank you!

P.s. This is the result of dd($from) :

array (size=2)  0 => string '2014-06-18 06:53:45' (length=19)  1 => string '2014-06-18 14:52:29' (length=19)
Pramod
  • 2,828
  • 6
  • 31
  • 40
John Evans Solachuk
  • 1,953
  • 5
  • 31
  • 67
  • Pardon me if I misunderstood you, but it looks to me like you want all messages with a later timestamp than the latest one in the $from array. If that is indeed the case, I'd say that the best practice would be calculating which that one timestamp is and supplying it to the query - or, ideally, only sending the latest one to the form instead of an array to begin with. – Joel Hinz Jun 18 '14 at 07:47
  • I've deleted the answer but these dates could be sorted using `asort` like functions and can get max value easily. – The Alpha Jun 18 '14 at 08:26
  • @WereWolf-TheAlpha thanks for the effort! Appreciate it. :) – John Evans Solachuk Jun 18 '14 at 08:30

1 Answers1

3

You need exactly what you have, but to find the latest date in the array and use that.

This can be done easily

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

This will return all messages where the created_at timestamp is greater than the latest date in the $from array.

Joe
  • 1,384
  • 10
  • 17
  • +1, This gives me dates in `desc` order but better yet. – The Alpha Jun 18 '14 at 08:27
  • 1
    @Joe Yes, of course! Why on earth would I want to compare 3 timestamps when all timestamps are always in order of each other and I could just compare the latest one? Wow, I feel quite silly now. Thank you very much! – John Evans Solachuk Jun 18 '14 at 08:28
  • Glad I could be of help. As a reference, this page on php.net is useful for array sorting and so on. http://www.php.net//manual/en/array.sorting.php – Joe Jun 18 '14 at 08:34
  • @Joe btw, what's the opposite of end()? – John Evans Solachuk Jun 18 '14 at 08:54
  • I believe something along these lines would work. `$first = array_shift(array_values($myArray));` or using `$first = reset($array);` may be a better solution. See: http://stackoverflow.com/questions/1921421/get-the-first-element-of-an-array – Joe Jun 18 '14 at 08:57