doumianfeng6979 2016-10-17 06:35
浏览 105
已采纳

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

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 :-

  1. public function Filter(Request $request)
  2. {
  3. $conditions = array();
  4. if($request->isMethod('post')) {
  5. $data = array_filter($request->all());
  6. $shapes = array();
  7. $amount = array();
  8. $carat = array();
  9. $cut = array();
  10. $color = array();
  11. $clarity = array();
  12. $cerifying_agency = array();
  13. $flourscence = array();
  14. $polish = array();
  15. $symmetry = array();
  16. // $conditions=array();
  17. if(isset($data['shape'])){
  18. $shapes = $data['shape'];
  19. }
  20. if(isset($data['amount'])){
  21. $amount = $data['amount'];
  22. }
  23. if(isset($data['carat'])){
  24. $carat = $data['carat'];
  25. }
  26. if(isset($data['cut'])){
  27. $cut = $data['cut'];
  28. }
  29. if(isset($data['color'])){
  30. $color = $data['color'];
  31. }
  32. if(isset($data['clarity'])){
  33. $clarity = $data['clarity'];
  34. }
  35. if(isset($data['ca'])){
  36. $cerifying_agency = $data['ca'];
  37. }
  38. if(isset($data['fl'])){
  39. $flourscence = $data['fl'];
  40. }
  41. if(isset($data['polish'])){
  42. $polish = $data['polish'];
  43. }
  44. if(isset($data['symmetry'])){
  45. $symmetry = $data['symmetry'];
  46. }
  47. }
  48. }

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

展开全部

  • 写回答

2条回答 默认 最新

  • duanche2007 2016-10-17 07:46
    关注

    You can use function similar to this:

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

    You can use it like this:

    1. $productQuery = DB::table("products");
    2. $productQuery = appendFilter($productQuery, $shapes, "shape");
    3. $productQuery = appendFilter($productQuery, $otherFilters, "otherField");
    4. $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部