1

So I have events table which has start date and end date.

What I want to do is a comparison between today and start date + end date so if today is between start and end, return that collection which will be displayed on page, if it isn't ignore that event.

Problem is that when I retrieve an event I cannot access it as it is return that it doesn't exist in the collection, but it does after view is returned.

Here's my controller:

public function show($id)
{
    $today = date("Y-m-d");
    $today_dt = new DateTime($today);
    $event = Event::with('businesses')
        ->get();
    $test = $event->startdate;
    $test2 = $event->enddate;
    //something like if $today is not less that start date and not higher than end date, return that collection?
    dd($test);
    return view('events.showEvent', compact('event'));
}
1
  • 1
    What column type are you using to store your dates? Commented Jul 4, 2017 at 15:40

3 Answers 3

1

use where date function like this

$today = Carbon::now();
$event = Event::with('businesses')
           ->whereDate('startdate', '<', $today->format('Y-m-d'))
           ->whereDate('enddate', '>', $today->format('Y-m-d'))
           ->get();
Sign up to request clarification or add additional context in comments.

Comments

0

If I understood your problem correctly, I think this should suffice:

$today = Carbon::today();
$event = Event::whereDate('startdate', '>', $today->format('Y-m-d'))
    ->whereDate('enddate', '<', $today->format('Y-m-d'))
    ->with('businesses')
    ->get();

I hope you did search the internet for this problem in the first place

4 Comments

unfortunately this brings me back 0 results :(
@PrzemekWojtas You can debug by testing what returns when you do only, Event::where('startdate', '>', $today->format('Y-m-d')) ->where('enddate', '<', $today->format('Y-m-d'))->get(); Look into your database and see a record you think should be returned, you can replace the $today to be your preferred date like: $today = Carbon::parse('22-01-2017'); for test purpose
@ShahzaibSheikh how do you mean? You mean putting $today->format('Y-m-d') in single quotes? Actually the format() method of Carbon resolves to string at least with little I have seen it do, or did I misunderstand you?
whereDate('start', '<', $today->format('Y-m-d')) ->whereDate('end', '>', $today->format('Y-m-d'))->get()use like this
0

in Model

public function scopeOfToday($query){
    $today = \Carbon\Carbon::today()->format('Y-m-d');
    return $query->whereRaw("? BETWEEN startdate and enddate",$today);
}

in Controller

public function show($id)
{
    $event = Event::ofToday()->with('businesses')->get();

    $test = $event->startdate;
    $test2 = $event->enddate;
    //something like if $today is not less that start date and not higher than end date, return that collection?
    dd($test);
    return view('events.showEvent', compact('event'));
}

2 Comments

I get that it doesn't exist in current collection
which one doesn't exist?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.