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

alihoushyaripour's avatar

how to write this mysql query using eloquent?

Hi, I have a model with name Game and I want to write a eloquent query with multiple Date where, I was write this query as raw but can't write it using eloquent to fetch data as model.

game model class:

{
    // other parameters
    ....
    timestamp created_at;
    timestamp updated_at;
    timestamp started_at;
    timestamp finished_at;
    timestamp deleted_at;
}

php code and mysql query:

$now = \Carbon\Carbon::now()->format('Y-m-d H:i:s');
$afterTomorrow = \Carbon\Carbon::today()->addDays(2)->format('Y-m-d H:i:s');
\DB::raw("select * from games where ( started_at >= $now and started_at <= $afterTomorrow ) or ( started_at <= $now and finished_at >= $now )")->getValue();

Anyone can help me?

0 likes
10 replies
gorakhyadav's avatar

use orwhere instead of or

$now = \Carbon\Carbon::now()->format('Y-m-d H:i:s'); $afterTomorrow = \Carbon\Carbon::today()->addDays(2)->format('Y-m-d H:i:s'); \DB::raw("select * from games where ( started_at >= $now and started_at <= $afterTomorrow ) orwhere ( started_at <= $now and finished_at >= $now )")->getValue();

LiamHammett's avatar

Something along these lines should do it:

$now = Carbon::now();
$afterTomorrow = Carbon::today()->addDays(2);

$games = Game::whereBetween('started_at', [$now, $afterTomorrow])
    ->orWhere(function($query) use ($now, $afterTomorrow) {
        $query->where('started_at', '<=', $now);
              ->orWhere('finished_at', '>=', $now);
    })
    ->get();
alihoushyaripour's avatar

@GORAKH - OK, but my query is like this:

( whereDate && whereDate ) || (whereDate && whereDate )

How and Where can I used orWhere in this structure? like this?

( whereDate && whereDate ) || (orWhereDate && whereDate )
gorakhyadav's avatar

Try something like this.

$users = DB::table('users') ->where('votes', '>', 100) ->orWhere('name', 'John') ->get();

alihoushyaripour's avatar

@GORAKH - I was write this first, but all of 4 whereDate executed as && together:

\App\Models\Game::whereNull('deleted_at')
    ->whereDate('started_at', '>=', $now)->whereDate('started_at', '<=', $afterTomorrow)
    ->whereDate('started_at', '<=', $now)->whereDate('finished_at', '>=', $now)
    ->orderBy('created_at', 'desc');

and then I was changed query to this:

\App\Models\Game::whereNull('deleted_at')
    ->whereDate('started_at', '>=', $now)->whereDate('started_at', '<=', $afterTomorrow)
    ->orWhereDate('started_at', '<=', $now)->whereDate('finished_at', '>=', $now)
    ->orderBy('created_at', 'desc');

but the first to the third query is executed correctly, but the fourth qurry doesn't executed correctly, because the first and second whereDate must be && together, and also the third and fourth whereDate query must be && together, and then the result of these two queries must be || together.

gorakhyadav's avatar

@alihoushyaripour

you can try this .

    $now = \Carbon\Carbon::now()->format('Y-m-d H:i:s');
    $afterTomorrow = \Carbon\Carbon::today()->addDays(2)->format('Y-m-d H:i:s');
   \DB::raw("select * from games whereBetween ( 'started_at',[$FromDate, $ToDate] ) orwhereBetween ( 
    'started_at',[$FromDate, $ToDate] )")->getValue();
1 like
gorakhyadav's avatar
Level 1

You want to like this ?

          \App\Models\Game::whereNull('deleted_at')
         ->whereBetween('started_at',[$now,$afterTomorrow] )
         ->WhereDate('started_at', '<=', $now)
          ->whereDate('finished_at', '>=', $now)
          ->orderBy('created_at', 'desc');
1 like
alihoushyaripour's avatar

@GORAKH - Yes my friend, I test now and both of them worked for me, thank you.

I set second answer as Best Answer because it's write using eloquent and be beauty and clean.

1 like

Please or to participate in this conversation.