Pick up records with date greater than the current - Laravel 5.1

Asked

Viewed 2,025 times

1

I need to list the schedules that have date greater or equal to the current date, I am doing the way below but does not work, it does not return any record.

Auth::user()->Agendamentos->where('data', '>=', date("Y-m-d h:i:s"))

I’m doing it in mine view. If I take that where works normally, but it lists everything, both with past date and with future date.

My field date stores the data in the format datetime (2017-07-01 10:25:45).

  • Your field on the bench is one DateTime really, do you have a way to put the layout of your table? , because looking at it like this, it seems correct @Raylan

  • Yes, that’s right. $table->datetime('date');

  • Look, there’s only one answer, but basically if you do this or use it the same as your answer has to work the same way maybe you need to test it like this Auth::user()->Agendas->Where('data', '>=', date("Y-m-d"))` and then tells me which result apparently gave, ie without time, minutes and seconds only the date?

  • also does not work. Strange that the query in phpmyadmin works correctly. SELECT * FROM agendamentos WHERE data >= '2017-06-01 11:29:57' AND medico_id = 1

  • Use like this: Auth::user()->Agendamentos()->where('data', '>=', date("Y-m-d h:i:s"))->get() and tell me what happened? That is instead of the list, will use Where in the method!

  • I changed the field type to date and compared using only date("Y-m-d"), but it doesn’t even work like that

  • I think I’ll take this in the controller with a raw...

  • how is your code in the controller?

  • There is nothing related to this in the controller, I’m picking up the relationship through the logged in user. Everything happens in the view with Auth::user()->Schedules

  • is very confused! and it seems wrong to me also in a certain way, lack context so of the great doubt, well who knows you putting more details ...

Show 6 more comments

1 answer

2

First ensure that your dates are handled correctly in the global format that uses Carbon: https://laravel.com/docs/5.4/eloquent-mutators#date-mutators ...

class SeuModelo {

    protected $dates = [
        'data',
    ];
    ...

The Where filter you can use Carbon:

Auth::user()->Agendamentos->where('data', '>=', \Carbon::now())
  • I tried that way, I treated Model and I used Carbon, but it still doesn’t work. I printed the 2 results, the date field and the Carbon are in the same format, I don’t know what the problem is. Note: I have Laravel 5.1

  • You can ask the question the result of the query with and without using Where?

Browser other questions tagged

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