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

Carosobin's avatar

hide and show on date

i have a program table that has relationship with contest table so program has many contest, i only want to show contest when start_at date has reached and also hide the contest at end_at date

 Schema::create('programs', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->longText('body');
            $table->dateTime('start_at');
            $table->dateTime('end_at');
            $table->timestamps();
        });
0 likes
3 replies
Swaz's avatar

Probably something like this:

https://laravel.com/docs/9.x/eloquent-relationships#constraining-eager-loads

Program::with(['contests' => function ($query) {
    $query->where('start_at', '<=', now());
    $query->where('end_at', '>=', now());
}])->find($programId);

OR

https://laravel.com/docs/9.x/eloquent-relationships#lazy-eager-loading

$program->load(['contests' => function ($query) {
    $query->where('start_at', '<=', now());
    $query->where('end_at', '>=', now());
}]);
Swaz's avatar

@Snapey Or the new withWhereHas method 💡, depending on OP's needs.

Please or to participate in this conversation.