小编Ahm*_*sam的帖子

Laravel 和 php 最好的多重搜索方法

我有一个控制器,可以从搜索表单中获取四个输入。

SearchController.php 代码

public function results(Request $request) {
    $text           = $request -> text;
    $pet            = $request -> pet;
    $category       = $request -> category;
    $city           = $request -> city;
    $searchArray    = [];
    if(empty($text) && empty($pet) && empty($category) && empty($city)) {
        Session::flash('danger', "You didn't select any search any search.");
        return redirect() -> back();
    }

    //SEARCH CODE HERE
}
Run Code Online (Sandbox Code Playgroud)

我想做什么

我正在尝试在我的数据库中搜索 4 列

问题是

我还需要在一个查询中搜索 4 列。

这意味着我需要检查$text变量是否不为空并且$pet变量不为空然后我必须执行以下查询:

if(!empty($text) && !empty($pet))
            $result = Post::where('text', 'like', '%'.$text.'%') -> where('text', $pet) …
Run Code Online (Sandbox Code Playgroud)

php mysql laravel eloquent

3
推荐指数
1
解决办法
5628
查看次数

标签 统计

eloquent ×1

laravel ×1

mysql ×1

php ×1