You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel双搜索表单故障:办公区搜索点击跳转首页问题

排查办公区搜索表单跳转首页的问题

咱们一步步梳理问题,你遇到的跳转首页问题核心是表单提交路径配置错误,还有几处代码细节问题,我帮你逐个修正:

1. 表单提交URL错误(最关键原因)

你办公区搜索表单的url参数设成了'/',这刚好是首页的路由地址,所以点击提交后自然会跳转到首页。同时表单的role属性也写错了,应该是search而非$searchoffice。

修正后的building.blade.php表单代码:

首先要给办公区搜索新增专属路由,然后修改表单指向这个路由:

{!! Form::open(['method'=> 'GET','route'=>'office.search','role'=>'search']) !!}
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="searchoffice" class="form-control" placeholder="Search offices...">
<span class="input-group-btn">
<button type="submit" class="btn btn-info btn-md">Search</button>
</span>
</div>
{!! Form::close()!!}

注意:你原来的按钮里多了一个无效的</i>标签,已经删掉避免HTML结构错误。

2. 新增办公区搜索路由

在路由配置文件里添加办公区搜索的专属路由:

// 新增办公区搜索路由,指向OfficeController的search方法
Route::get('office/search', 'OfficeController@search')->name('office.search');

3. 修正OfficeController的代码问题

你当前的OfficeController代码有几处明显错误:

  • 视图名不能用引号包裹变量'$offices',应该写实际的视图文件名(比如'offices')
  • 数据赋值的键名不对,$data['building'] = $offices应该改成$data['offices'],方便视图调用
  • 建议和首页搜索一样加入分页,提升用户体验

修正后的OfficeController.php代码:

public function search()
{
    $searchoffice = \Request::get('searchoffice');
    // 加入分页和排序,和首页建筑搜索保持一致的体验
    $offices = Office::where('name','like','%'.$searchoffice.'%')
                    ->orderBy('id', 'asc')
                    ->paginate();
    // 把办公区数据传递给视图,键名和视图里的变量对应
    return view('offices', ['offices' => $offices]);
}

如果你需要在某个建筑详情页搜索该建筑下的办公区,可以把路由改成Route::get('building/{id}/office/search', 'OfficeController@search')->name('building.office.search'),然后表单里新增隐藏字段传递建筑ID,控制器里加上where('building_id', $id)的条件即可。

这样修改后,点击办公区搜索按钮就会跳转到OfficeController的search方法处理搜索逻辑,不会再跳回首页了。


内容的提问来源于stack exchange,提问作者TeamPlar Jarj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:47:19