2

I use Laravel advanced wheres in Lumen for a query in MongoDB and i use jenssegers/laravel-mongodb package for Lumen and my query is :

$time_5_min_ago = Carbon::now()->subMinute(5);
$time_10_min_ago = Carbon::now()->subMinute(10);
$time_15_min_ago = Carbon::now()->subMinute(15);

return Order::whereBetween('source_longitude', [$minLon_try_one, $maxLon_try_one])
    ->whereBetween('source_latitude',[$minLat_try_one,$maxLat_try_one])
    ->where('status','=','suspend')
    ->where('created_at','<=',$time_5_min_ago)
    ->where('created_at','>=',$time_10_min_ago)
    ->orWhere(function($query) use ($maxLat_try_two,$minLat_try_two,$maxLon_try_two,$minLon_try_two,$time_10_min_ago,$time_15_min_ago)
    {
        $query->whereBetween('source_longitude', [$minLon_try_two, $maxLon_try_two])
        ->whereBetween('source_latitude',[$minLat_try_two,$maxLat_try_two])
        ->where('status','=','suspend')
        ->where('created_at','<=',$time_10_min_ago)
        ->where('created_at','>=',$time_15_min_ago);
    }
    )->get();

But when i run this query, i got no result and i don't know how fix it?

fusion3k
  • 11,568
  • 4
  • 25
  • 47
bitcodr
  • 1,395
  • 5
  • 21
  • 44
  • Maybe no result is simply the result of your query? If you get no error there is nothing wrong with the code. Try building up the query slowly, step by step. – Nicklas Kevin Frank Apr 05 '16 at 19:19
  • But I Got result when remove orWhere function query. – bitcodr Apr 05 '16 at 20:21
  • I mean the first part of query is not dependent with orWhere function query. So why not show result – bitcodr Apr 05 '16 at 20:26
  • Please help me friends – bitcodr Apr 06 '16 at 03:13
  • Anybody know to help me – bitcodr Apr 06 '16 at 06:24
  • And when you run both queries individually they work? Could you try to add a `->toSql()` and show us what query is generated? – Nicklas Kevin Frank Apr 06 '16 at 07:44
  • i got `"data": "select * from \"orders\" where \"source_longitude\" between ? and ? and \"source_latitude\" between ? and ? and \"status\" = ? and \"created_at\" <= ? and \"created_at\" >= ? or (\"source_longitude\" between ? and ? and \"source_latitude\" between ? and ? and \"status\" = ? and \"created_at\" <= ? and \"created_at\" >= ?)"` – bitcodr Apr 06 '16 at 07:48

1 Answers1

0

I found the solution . This works :

    $time_5_min_ago = Carbon::now()->subMinute(5);
    $time_10_min_ago = Carbon::now()->subMinute(10);
    $time_15_min_ago = Carbon::now()->subMinute(15);
    $time_20_min_ago = Carbon::now()->subMinute(20);

    return Order::where(function ($query)  use ($maxLat_try_one,$minLat_try_one,$maxLon_try_one,$minLon_try_one,$time_5_min_ago,$time_10_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_one, $maxLon_try_one])
            ->whereBetween('source_latitude', [$minLat_try_one,$maxLat_try_one])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_5_min_ago)
            ->where('created_at', '>=', $time_10_min_ago);
    })->orWhere(function ($query)  use ($maxLat_try_two,$minLat_try_two,$maxLon_try_two,$minLon_try_two,$time_10_min_ago,$time_15_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_two, $maxLon_try_two])
            ->whereBetween('source_latitude', [$minLat_try_two,$maxLat_try_two])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_10_min_ago)
            ->where('created_at', '>=', $time_15_min_ago);
    })->orWhere(function ($query)  use ($maxLat_try_three,$minLat_try_three,$maxLon_try_three,$minLon_try_three,$time_15_min_ago,$time_20_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_three, $maxLon_try_three])
            ->whereBetween('source_latitude', [$minLat_try_three,$maxLat_try_three])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_15_min_ago)
            ->where('created_at', '>=', $time_20_min_ago);
    })->get($fields);
bitcodr
  • 1,395
  • 5
  • 21
  • 44