简体   繁体   English

如何从具有多个数组条件的laravel中的表中检索数据

[英]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. 对于一个数组条件,我可以在laravel中使用whereIn但是对于多个数组条件我应该使用什么。我不确定哪个数组将为空。

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 我已经附加了UI的快照和ajax的数据格式,它正在过滤函数的数组中被调用: - 抱歉我无法嵌入图片,因为Stackoverflow不允许我这样做

Well, you can use those conditions just like regular whereIn . 好吧,你可以像常规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. 这个函数做的是它附加嵌套在哪里与第一部分做WhereIn ,但它也有orWhereRaw,如果数组为空,则附加原始“true”值 (否则 false)因此,如果你的数组带有过滤值为空,它将不会影响您选择的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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