vendredi 6 juillet 2018

Laravel search issue in relationship

I have search functionality in laravel

$product=ProductItem::where(function ($query)use($fromDate,$toDate){
            if($fromDate!= ''&& $toDate!=''){
                $query->whereBetween(DB::raw("DATE(purchased_date)"),[$fromDate,$toDate]);
            }
        })->with(['itemName'=>
            function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='item_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            }
        ,'itemBrand'=> function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='brand_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            },'itemModel'=>function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='item_model_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            },'vendor'=>function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='vendor_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            }])




            ->get();

the above code if i search for itemName then it will return all records with emtpy itemNames for other than search items enter image description here In the screenshot i have searched for itemName nargis but it returned other itemnames with empty itemName and also i tried whereHas nut if foreign key null then it wont return any records

 $product=ProductItem::where(function ($query)use($fromDate,$toDate){
            if($fromDate!= ''&& $toDate!=''){
                $query->whereBetween(DB::raw("DATE(purchased_date)"),[$fromDate,$toDate]);
            }
        })->with(['itemName','itemBrand','itemModel','vendor'])
            ->whereHas('itemName', function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='item_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            })
            ->whereHas('itemModel', function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='item_model_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            })
            ->whereHas('itemBrand', function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='brand_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            })
            ->whereHas('vendor', function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='vendor_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            })
            ->get();

Can any one help me how i can add search functionality in laravel relationship



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire