mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7.02k stars 1.43k forks source link

Query Not working with orWhere closure. #2301

Open Dasinfomedia2 opened 3 years ago

Dasinfomedia2 commented 3 years ago

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)
sam-script commented 3 years ago

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.

Dasinfomedia2 commented 3 years ago

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.

begbaj commented 2 years ago

Any updates on this? I have the same problem