简体   繁体   中英

How can I retrieve data from a table in laravel with multiple array conditions

Im applying a search filter in a e-commerce website. Here the Customer can choose various fields like color, shape, price, symmetry,polish,clarity and various other things. While the Customer selects any option it should add to the last option he have selected.So he/she may have combination of condition which have to be fetched and shown. For one array condition i can use whereIn in laravel but for multiple array conditions what should I use.Im not sure which array will be empty.

Here is my code for filter :-

public function Filter(Request $request)
{
    $conditions = array();
    if($request->isMethod('post')) {
        $data = array_filter($request->all());

        $shapes = array();
        $amount = array();
        $carat  = array();
        $cut  = array();
        $color = array();
        $clarity = array();
        $cerifying_agency = array();
        $flourscence = array();
        $polish = array();
        $symmetry = array();
       // $conditions=array();
        if(isset($data['shape'])){
            $shapes = $data['shape'];
        }
        if(isset($data['amount'])){
            $amount = $data['amount'];
        }
        if(isset($data['carat'])){
            $carat = $data['carat'];
        }
        if(isset($data['cut'])){
            $cut = $data['cut'];
        }
        if(isset($data['color'])){
            $color = $data['color'];
        }
        if(isset($data['clarity'])){
            $clarity = $data['clarity'];
        }
        if(isset($data['ca'])){
            $cerifying_agency = $data['ca'];
        }
        if(isset($data['fl'])){
            $flourscence = $data['fl'];
        }
        if(isset($data['polish'])){
            $polish = $data['polish'];
        }
        if(isset($data['symmetry'])){
            $symmetry = $data['symmetry'];
        }

    }
}

I have attached the snapshot of the UI and the data format for ajax which is being called in the array of the filter function :- Sorry I couldnt embed the picture as Stackoverflow is not allowing me to do so

Well, you can use those conditions just like regular whereIn .

For example:

$posts = Post::latest();

if($request->has('carat')) 
{
    $posts->whereIn('carat', $request->input('carat');
}

if($request->has('shape')) 
{
    $posts->whereIn('shape', $request->input('shape'));
}

....

$posts = $posts->get();

Be sure to validate your conditions first.

You can use function similar to this:

function appendFilter($query, $array, $fieldName){
    return $query->where(function($query) use ($array, $fieldName) {
        $query->whereIn($fieldName, $array)->orWhereRaw(empty($array) ? "true" : "false");
    });
}

You can use it like this:

$productQuery = DB::table("products");
$productQuery = appendFilter($productQuery, $shapes, "shape");
$productQuery = appendFilter($productQuery, $otherFilters, "otherField");
$results = $productQuery->get();

What this function does is it appends nested where with the first part that does WhereIn , but it also has orWhereRaw which appends raw "true" value in case array is empty (false otherwise) So in case your array with filtered values is empty it will not affect the values in your select.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM