Search records between two dates using the eloquent of the Standard

Asked

Viewed 535 times

0

I need to search the bank records between two dates.

example:

$emprestimos_material_qtd = MultimeiosDetalheEmprestimo::
      where('recursoId', 2)
       ->where('dataDevolucao', null)
       ->whereDate('dataEmprestimoSaida', '>=', '2019-02-07 16:00:00')
       ->whereDate('dataEmprestimoDevolucao', '<=', '2019-02-14 15:06:00')
       ->get();

In the database I have the following records

(7, 12, 2, 'agendado', '2019-02-07 16:00:00', '2019-02-09 13:00:00', NULL, '10.1.2.113', NULL, 182, NULL, 3, NULL, '2019-02-04 19:04:36', '2019-02-04 19:04:36'),
(10, 13, 2, 'agendado', '2019-02-07 16:00:00', '2019-02-09 13:00:00', NULL, '10.1.2.113', NULL, 182, NULL, 3, NULL, '2019-02-04 19:05:37', '2019-02-04 19:05:37'),
-- (13, 15, 2, 'agendado', '2019-02-13 15:06:00', '2019-02-14 15:06:00', NULL, '10.1.2.113', NULL, 182, NULL, 3, NULL, '2019-02-05 17:15:38', '2019-02-05 17:15:38'),
-- (14, 15, 2, 'agendado', '2019-02-13 15:06:00', '2019-02-14 15:06:00', NULL, '10.1.2.113', NULL, 182, NULL, 3, NULL, '2019-02-05 17:15:38', '2019-02-05 17:15:38'),

NOTE: The first date is the date field.

In my opinion he should bring the 4 records, but he brings only 2, the first that has equal values of the field datePress to the query is not returned.

The records on marked is selected , the others not despite meet the rule of equality.

Note: Same case I change the search to $emprestimos_material_qtd = Multimeiosdetailseemprestimo: Where('recursoId', 2) ->Where('dataDevolution', null) ->whereDate('dataEmprestimoSaida', '>=', '2019-02-07 16:00:00') ->get();

Continues to bring only the last two table records

  • The condition was to see if dataEmprestimoSaida is greater than the value and also dataEmprestimoDevolucao minor or check a condition or other ?

  • It is to check the two conditions, that is to search for all the records that are in this interval

  • but there is no interval, the condition (the query says so) it will return all records that are 'dataEmprestimoSaida', '>=', '2019-02-07 16:00:00'and who also have 'dataEmprestimoDevolucao', '<=', '2019-02-14 15:06:00'

  • Ideal and you mount the sql and see if it responds as it should, which and the column dataEmprestimoSaida and dataEmprestimoDevolucao

  • The first date is the Date Date and the second date is the Date Date. Even if I leave only the first condition that is ->whereDate('dateEmprestimoSaida', '>=', '2019-02-07 16:00:00') it will continue to bring only the last two records, ie the ones that are equal it does not return.

  • I’ll answer a test I did here see if it will solve your problem.

Show 1 more comment

1 answer

0

According to what I was reading on documentation the WhereDate(); it only compares dates and not the timestamp, for example it would be the same thing as putting a where DATE(created_at) >= .... the ideal would be to use only the where for example in your case:

$emprestimos_material_qtd = MultimeiosDetalheEmprestimo::
      where('recursoId', 2)
       ->where('dataDevolucao', null)
       ->where('dataEmprestimoSaida', '>=', '2019-02-07 16:00:00')
       ->where('dataEmprestimoDevolucao', '<=', '2019-02-14 15:06:00')
       ->get();

Test there and see if you will behave properly.

  • behaved correctly, functioning in the expected manner.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.