laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Query Not working with orWhere closure.

Open Dasinfomedia2 opened this issue 4 years ago • 3 comments

  • Laravel-mongodb Version: 3.8
  • PHP Version: 7.3.26
  • Database Driver & Version: mongodb

Description:

Query is not performing as it should

Steps to reproduce

$todayStart = new DateTime(date('Y-m-d'));
$todayEnd = new DateTime(date('Y-m-d').'+1 day');

$tasks = $this->tasks()
                       ->whereBetween('due_date',[$todayStart, $todayEnd])
                        ->orWhere(function($query) use ($todayStart){
                            return $query->whereDay('due_date','<>',date('d'))
                                ->whereNotNull('start_date')
                                ->where('start_date', '<', $todayStart);
                        })
                        ->whereNull('end_date')
                        ->get();

Expected behaviour

-> it should return all tasks as per condition. -> All todays pending task and future due date tasks that has been started and still pending. -> When get ->toSql() and run it in mysql DB just for test. it returns expected result.But in MongoDB.

Actual behaviour

-> returns NULL

Logs: Insert log.txt here (if necessary)

Dasinfomedia2 avatar Jul 31 '21 05:07 Dasinfomedia2

Try creating your own base model class, which extends Jenssegers\Mongodb\Eloquent\Model, now all your new models can extend your new base model which you can fill with recurring functions, such as any eloquent methods you find that don't work as expected.

Within that new model, add any functions necessary that don't currently work, for example, I have a working example of the updateOrCreate eloquent method in my new base model, as I have referenced in #2299.

I've found this to be the best way for a quick solution. Any more eloquent methods I come across that don't work will be made in my new base model class to use across my models.

sam-script avatar Jul 31 '21 19:07 sam-script

I get your point. I think this package needs to be updated with more eloquent functions. i hope i can do PR.

Thanks for the reply.

Dasinfomedia2 avatar Aug 03 '21 09:08 Dasinfomedia2

Any updates on this? I have the same problem

begbaj avatar Sep 14 '22 18:09 begbaj