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

Using orWhere query causes problem in the search.

Open DarkB0SsS opened this issue 5 years ago • 4 comments

Summary of problem or feature request

I am using "orWhere" clause in my Laravel application where I build the datatable query. The results I get are perfect, but when I try to use the search it is messing up the results, no errors occur, but the results are not accurate at all.

Code snippet of problem

This is my query:

$data = Result::where(function($query) {
                                  $query->where('type', 'news');
                              })
                             ->orWhere(function($query) {
                                 $query->where('type', 'editorials')
                                       ->where('year', '>=', 2018);
                             })
                             ->select('results.*');

If I remove the orWhere clause, the search is fixed. The problem is not from the callback function it is from the orWhere. I am not sure how to fix this. Any suggestions will be appreciated.

System details

  • Ubuntu 16.04
  • PHP 7.2
  • Laravel 5.8.13
  • yajra/laravel-datatables-oracle: "^9.0"

DarkB0SsS avatar Apr 12 '20 07:04 DarkB0SsS

I think the prob is on your query. Maybe try to chain orWhere inside one closure instead.

$data = Result::where(function($query) {
                                  $query->where('type', 'news')
                             ->orWhere(function($query) {
                                 $query->where('type', 'editorials')
                                       ->where('year', '>=', 2018);
                             })
                             ->select('results.*');

yajra avatar Apr 14 '20 01:04 yajra

Even if I make the query like this:

$data = Result::where('type', 'news')->orWhere('type', 'editorials')->select('results.*');

The search problem remains (the above query is just to test it). The orWhere clause causes the problem I think. The thing is I need orWhere clause so I can exclude all the editorials before 2018, so I have to use it.

I followed your suggestion. With a query such as this:

$data = Result::where(function($query) {
                        $query->where('type', 'news');
                        $query->orWhere(function($query_two) {
                           $query_two->where('type', 'editorials')
                                     ->where('year', '>=', 2018);
                           });
                     })
                     ->select('results.*');

So indeed the problem comes if orWhere is in the query. But if orWhere is in the callback, the problem disappears.

Thank you for the suggestion. Hope this also helps to someone else! :)

DarkB0SsS avatar Apr 15 '20 06:04 DarkB0SsS

This helped me too, thank you.

meckyblaze avatar Feb 04 '21 01:02 meckyblaze

Thank for the help, trying to combine filter and orWhere didn't work !

Good Job

vassilidev avatar Jun 06 '22 15:06 vassilidev

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jan 30 '23 00:01 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Feb 06 '23 00:02 github-actions[bot]