Home > other >  I want to filter single value or multiple value in Laravel controller
I want to filter single value or multiple value in Laravel controller

Time:07-11

I want to filter one value or multiple values in the Laravel controller

$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
            ->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
            ->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
            ->where('district', 'LIKE', '%' . $district . '%')
            ->where('news_paper_city', 'LIKE', '%' . $city . '%')
            ->where('machine_field', 'LIKE', '%' . $machine_field .'%')
            ->get();

I want to filter this query past district value(only district data)
city = only city data
machine field = only machine field data
pass district and city = only district and city data
pass district and machine field= only district and machine field data
pass district, city and machine field = only district, city and machine field data

Please help me solve this problem.

CodePudding user response:

 $news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
            ->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
            ->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
            ->where(function($query) use($district,$city,$machine_field){
                if($district){
                    $query->where('district', 'LIKE', '%' . $district . '%');
                }
                if($city){
                    $query->where('news_paper_city', 'LIKE', '%' . $city . '%');
                }
                
                if($machine_field){
                    $query->where('machine_field', 'LIKE', '%' . $machine_field . '%');
                }
                

            })
            ->get();

CodePudding user response:

use when and OrWhere query like this

    $news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
                ->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
                ->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
                ->when($district, function ($query) use ($district) {
                     return $query->orWhere('district', 'LIKE', '%' . $district . '%');
                })
                ->when($city, function ($query) use ($city) {
                     return $query->orWhere('news_paper_city', 'LIKE', '%' . $city . '%');
                })
                ->when($machine_field, function ($query) use ($machine_field) {
                     return $query->orWhere('machine_field', 'LIKE', '%' . $machine_field .'%');
                })->get();
  • Related