【问题标题】:How to display just "searched" data on the page in table?如何在表格中的页面上仅显示“搜索”数据?
【发布时间】:2018-10-11 09:12:05
【问题描述】:

我制作了搜索表单并尝试通过搜索来检索数据。但在我开始搜索页面上已经检索到的数据之前。因为我已经用这种方式制作了控制器。当我删除该检索功能时,页面运行只是白屏。所以问题就在这里,我想在搜索之后而不是之前看到表和数据检索。但对如何做或做错感到烦恼。

这里是控制器:

public function welcome()
{

    $estates = Estates::orderBy('price')->get();

    $data['estates'] = $estates;
    return view('welcome', $data);

}

public function search(Request $request)
{
    $q = $request->q;
    if (trim($q) !== ""){//here

        $estates = \DB::table('estates')->where("name","LIKE", "%" . $q . "%")
            ->orWhere("address","LIKE", "%" . $q . "%")
            ->get();

        dd($estates);

        if(count($estates) > 0){
            return view("welcome", compact('estates'))->withQuery($q);
        }

    }

    $estates = array();//here
    return view("welcome", compact('estates'))->withMessage("No Found!");//here
}

还有我的路线:

Route::get("/", "PagesController@welcome");

Route::post("/search", "PagesController@search")->name('search.route');

此外,当我尝试搜索时,页面不是以表格形式出现,而是像这样。

Collection {#221 ▼
  #items: array:1 [▶]
}

如果您需要查看我的查看页面,我也可以添加它。 感谢您的帮助!

【问题讨论】:

  • 移除dd($estates);以显示视图
  • 感谢您的小小的触摸解决了一个问题。但仍在检索页面上的所有数据,无需搜索。

标签: php database laravel search-form


【解决方案1】:

我解决了这个问题:

public function welcome()
    {

        $estates = array();//here

        $data['estates'] = $estates;
        return view('welcome', $data);

    }

    public function search(Request $request)
    {
        $q = $request->q;
        if ($q !== null && trim($q) !== ""){//here

            $estates = \DB::table('estates')
                ->where("name","LIKE", "%" . $q . "%")
                ->orWhere("address","LIKE", "%" . $q . "%")
                ->orWhere("company_name","LIKE", "%" . $q . "%")
                ->orderBy('price')->get();

            if(count($estates) > 0){
                return view("search", compact('estates'))->withQuery($q);
            }

        }

        $estates = array();//here
        return view("search", compact('estates'))->withMessage("No Found!");//here
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 2023-03-27
    • 2022-11-14
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多