dongqie2010 2017-02-01 09:33
浏览 144

PHP Laravel使用多个输入中的多个关键字搜索多个表

I have two tables in db, "deals" and "deals_meta" connected between them with a foreign key. In the "deals" table i have general information about a deal like, title, description, category etc., and in the "deals_meta" i have other specific details like, location, number of bedrooms, price, booking date and so on.

I have a form with multiple fields, title, details, booking date, price range and so on.

My problem is that i can't retrieve from db the deals based on the search query.

The code that i use to retrieve the queries looks like this:

$deals = Deals::where('deals_providers', $provider)->where('deal_types', $dealType)->with('meta')
            ->where(function ($query) use ($terms, $dealType) {
                $query->whereHas('meta', function ($query) use ($terms, $dealType) {
                    $search = $query->where('deal_type', 'like', '%' . $dealType . '%');
                    if ($terms['location']) {
                        $search = $query->orWhere('location', 'like', '%' . $terms['location'] . '%');
                    }
                    if ($terms['price_from']) {
                        $search = $query->orWhere('price', '>', '%' . $terms['price_from'] . '%');
                    }
                    if ($terms['price_to']) {
                        $search = $query->orWhere('price', '<', '%' . $terms['price_to'] . '%');
                    }
                    if ($terms['departs_from']) {
                        $search = $query->orWhere('departs_from', 'like', '%' . $terms['departs_from'] . '%');
                    }
                    if ($terms['duration']) {
                        $search = $query->orWhere('duration', 'like', '%' . $terms['duration'] . '%');
                    }
                    if ($terms['available_start']) {
                        $search = $query->orWHere('available_start', 'like', '%' . $terms['available_start'] . '%');
                    }
                    if ($terms['available_end']) {
                        $search = $query->orWhere('available_end', 'like', '%' . $terms['available_end'] . '%');
                    }

                    return $search;
                });
                if ($terms['title']) {
                    $query->orWhere('title', 'like', '%' . $terms['title'] . '%');
                }
                if ($terms['details']) {
                    $query->orWhere('details', 'like', '%' . $terms['details'], '%');
                }
            })->get();

        Debug::data($deals);

If i search for title or details, it returns only the deals that have that title or contains that details but if i search for location or any other meta related term, it returns all the deals results from the db. What i'm doing wrong or how can i search corectly?

Thank you!

  • 写回答

1条回答 默认 最新

  • douxiapi4381 2017-02-01 10:42
    关注

    Solved!

    This is the code that works for my case!

    $deals = Deals::where('deals_providers', $provider)->where('deal_types', $dealType)->with('meta')
            ->where(function ($query) use ($terms, $dealType) {
                $query->whereHas('meta', function ($query) use ($terms, $dealType) {
                    if ($terms['location']) {
                        $query->where('location', 'like', '%' . $terms['location'] . '%');
                    }
                    if ($terms['price_from'] && $terms['price_to']) {
                        $query->whereBetween('price', [$terms['price_from'], $terms['price_to']]);
                    }
                    if ($terms['departs_from']) {
                        $query->where('departs_from', 'like', '%' . $terms['departs_from'] . '%');
                    }
                    if ($terms['duration']) {
                        $query->where('duration', 'like', '%' . $terms['duration'] . '%');
                    }
                    if ($terms['available_start']) {
                        $query->where('available_start', 'like', '%' . $terms['available_start'] . '%');
                    }
                    if ($terms['available_end']) {
                        $query->where('available_end', 'like', '%' . $terms['available_end'] . '%');
                    }
                });
                if ($terms['title']) {
                    $query->orWhere('title', 'like', '%' . $terms['title'] . '%');
                }
                if ($terms['details']) {
                    $query->orWhere('details', 'like', '%' . $terms['details'], '%');
                }
            })->get();
    
        Debug::data($deals);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state