Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GreyhoundIT's avatar

Getting the next date from a table

I am trying to make a show page for a zone which will display a list of fixtures.

class UserZoneController extends Controller { public function show($id) { $zone = Zone::findOrFail($id); $fixtures = $zone->fixtures; $fixtures = $fixtures->sortBy('fixture_date');

    return view('zone.show')->withZone($zone)->withFixtures($fixtures);
}

}

The above code works so depending on which zone id i use teh show page will display a list of fixtured in date order.

I am now trying to make a $nextFixture variable psode code is $nextFixture = $fixtures->where('fixture_date', '>', now) and get the first one.

I have tried varitions of

public function show($id) { $zone = Zone::findOrFail($id); $fixtures = $zone->fixtures; $fixtures = $fixtures->sortBy('fixture_date');

    $next = $fixtures->where('fixture_date', '>', Carbon::today()->toDateString()); // returns null
$next = $fixtures->where('fixture_date', '<', Carbon::today()->toDateString()); // returns null

$next = $fixtures->where('fixture_date', '>', Carbon::today()->toDateString()); // returns null
$next = $fixtures->where('fixture_date', '<', Carbon::today()->toDateString()); // returns nul

    dd($next);

    return view('zone.show')->withZone($zone)->withFixtures($fixtures);
} 

a dd($fixtures) returns ollection {#234 ▼ #items: array:3 [▼ 0 => Fixture {#243 ▼ #fillable: array:8 [▶] #dates: array:1 [▶] #connection: null #table: null #primaryKey: "id" #keyType: "int" #perPage: 15 +incrementing: true +timestamps: true #attributes: array:11 [▼ "id" => "1" "zone_id" => "1" "club_id" => "6" "fixture_date" => "2016-12-29 00:00:00" "fixture_time" => "7PM" "start_sheet_skeleton" => null "start_sheet_official" => null "team_overall" => null "person_overall" => null "created_at" => "2016-12-27 18:24:17" "updated_at" => "2016-12-29 08:30:32" ] #original: array:11 [▶] #relations: [] #hidden: [] #visible: [] #appends: [] #guarded: array:1 [▶] #dateFormat: null #casts: [] #touches: [] #observables: [] #with: [] #morphClass: null +exists: true +wasRecentlyCreated: false } 1 => Fixture {#244 ▶} 2 => Fixture {#245 ▶} ] }

Any ideas?

0 likes
9 replies
GreyhoundIT's avatar

class Fixture extends Model { protected $fillable =['zone_id', 'club_id', 'fixture_date', 'fixture_time', 'start_sheet_skeleton', 'start_sheet_official', 'team_overall', 'person_overall']; protected $dates = ['fixture_date'];

.... }

migration

public function up() { Schema::create('fixtures', function (Blueprint $table) { $table->increments('id'); $table->integer('zone_id')->unsigned()->index(); $table->integer('club_id')->unsigned()->index(); $table->date('fixture_date')->nullable(); $table->string('fixture_time')->nullable(); $table->string('start_sheet_skeleton')->nullable(); $table->string('start_sheet_official')->nullable();; $table->string('team_overall')->nullable(); $table->string('person_overall')->nullable(); $table->timestamps(); }); }

and this is a value in the fixture_date colunm in database 2016-12-30 00:00:00

vmitchell85's avatar

Try this:

$next = $fixtures->where('fixture_date', '>', Carbon::now());
vmitchell85's avatar

@GreyhoundIT And you are sure you have fixture_date items with a date after now?

If you switch the sign from > to < do you get data back?

GreyhoundIT's avatar
GreyhoundIT
OP
Best Answer
Level 2

No data back att all changing from > to < $next = $fixtures->where('fixture_date', '>', Carbon::now()); // returns nothing $next = $fixtures->where('fixture_date', '<', Carbon::now());// returns nothing

I have had to do it like this

$zone = Zone::findOrFail($id);
    $fixtures = $zone->fixtures;
    $fixtures = $fixtures->sortBy('fixture_date');
   // $next = $fixtures->where('fixture_date', '>', Carbon::now());

    $next = $fixtures->filter(function ($fixture) {
        return $fixture['fixture_date'] > Carbon::now();
    });
    $next = $next->first();


    return view('zone.show')->withZone($zone)->withFixtures($fixtures)->withNext($next);
vmitchell85's avatar

I wonder if the issue might have been the lack of ->get() after the where... regardless... happy you found something that works!

GreyhoundIT's avatar

thanks I tried ->get() and ->get(0) and it wanst happy i haad a read through the documentation and the where() in collections just seamed to take 2 attributes eg ->where('name', 'Bob') so I think its more a is equal to comparison rathere then having greater then or less then but I could be wrong

vmitchell85's avatar

Well it's working now... sorry I wasn't more of a help...

Please or to participate in this conversation.