0

I want to filter with Free Text that mean user can type anything they want to filter. So, when user type in side the input as I put below. So, I want to filter them by following this logic.


Free text = "abc def,ghi jkl"
...
AND
(
    (report.name like '%abc%'  AND report.name like '%def%')
    OR
    (bom_functions.name like '%abc%'  AND bom_functions.name like '%def%')
    OR
    (report.name like '%ghi%' AND report.name like '%jkl%')
    OR
    (bom_functions.name like '%ghi%' AND bom_functions.name like '%jkl%')
)

Can anyone help me out!

Chantha
  • 27
  • 6

2 Answers2

1

Try below code it should work

$model->where(function ($query) {

        return $query
            ->where(function ($a) {
                return $a
                    ->where('report.name', 'like', '%abc%')
                    ->where('report.name', 'like', '%def%');
            })
            ->orWhere(function ($b) {
                return $b
                    ->where('bom_functions.name', 'like', '%abc%')
                    ->where('bom_functions.name', 'like', '%def%');
            })
            ->orWhere(function ($c) {
                return $c
                    ->where('report.name', 'like', '%abc%')
                    ->where('report.name', 'like', '%def%');
            })
            ->orWhere(function ($d) {
                return $d
                    ->where('bom_functions.name', 'like', '%ghi%')
                    ->where('bom_functions.name', 'like', '%jkl%');
            });
    });

Reference:Logical Grouping

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
Osama Alvi
  • 659
  • 4
  • 14
0

I didn't really get your question correctly but i think this can help you. You can use orWhere in Laravel

$freetext = "abc def,ghi jkl"
....
where(function ($query) use ($freetext) {
   $query->orWhere('report.name', 'LIKE', '%' . $freetext . '%')
         ->orWhere('bom_functions.name', 'LIKE', '%' . $freetext . '%')
}
princeallan
  • 55
  • 1
  • 7