通过Laravel中的Ajax请求过滤数据

时间:2018-10-01 17:22:13

标签: php mysql ajax laravel eloquent

这是我在控制器中的功能

public function ajaxResponse (Request $request) {
    if ($request->ajax()) {
        $mat_id = $request->input('mat');
        $cat_id = $request->input('cat');
        $met_id = $request->input('met');
        if ($cat_id != null) {
            $products = Product::whereHas("categories", function ($query) use ($cat_id) {
                $query->whereIn('category_id', explode(',', $cat_id));
            })->get();
        }
        if ($mat_id != null) {
            $products = Product::whereHas("productMaterial", function ($query) use ($mat_id) {
                $query->whereIn('product_material_id', explode(',', $mat_id));
            })->get();
        }

        if ($met_id != null) {
            $products = Product::whereHas("productionMethod", function ($query) use ($met_id) {
                $query->whereIn('production_method_id', explode(',', $met_id));
            })->get();
        }
        if ($cat_id == null && $mat_id == null && $met_id == null) {
            $products = Product::all();
        }
        $prod = view('partials.ajaxProducts', ['products' => $products])->render();
    }
    return response()->json(['prod' => $prod]);
}

根据$cat_id$mat_id$met_id过滤的所有记录 这里的类别和 productMaterial 与产品有很多关系,而 productionMethod 与产品有一对多关系 我想结合所有这三个关系通过ajax请求过滤数据

2 个答案:

答案 0 :(得分:0)

您可以使用:

public function ajaxResponse(Request $request) {
    if ($request->ajax()) {
        $mat_id = $request->input('mat');
        $cat_id = $request->input('cat');
        $met_id = $request->input('met');

        $products = Products::query(); 
        if ($cat_id != null) {
            $products = $products->whereHas("categories", function ($query) use ($cat_id) {
                $query->whereIn('category_id', explode(',', $cat_id));
            });
        }
        if ($mat_id != null) {
            $products = $products->whereHas("productMaterial", function ($query) use ($mat_id) {
                $query->whereIn('product_material_id', explode(',', $mat_id));
            });
        }
        if ($met_id != null) {
            $products = $products->whereHas("productionMethod", function ($query) use ($met_id) {
                $query->whereIn('production_method_id', explode(',', $met_id));
            });
        }

        $products = $products->get();

        $prod = view('partials.ajaxProducts', ['products' => $products])->render();
    }
    return response()->json(['prod' => $prod]);
}

答案 1 :(得分:0)

 function ShowFile(strPath){
    if (objHttpFileDataRequest){
        objHttpFileDataRequest.abort();
    }
    objHttpFileDataRequest = $.get("includes/main.cfm",
    {
        path: encodeURI(strPath)
    },
    function(strFileData){
        openFile(strFileData); //assuming strFileData is a string with a URI path
    });
}

var objHttpFileDataRequest = null;

function openFile(file) {
    var extension = file.substr( (file.lastIndexOf('.') +1) );
    switch(extension) {
        case 'txt':
        case 'log':
            $("#readerArea pre#filecontent").text(strFileData);
        break;     

        default:
            window.open(strFileData, "_blank");  //is not plain text, open in new window - this may be blocked by pop-up blocker protection in some browsers
    }
};