duanmie9741 2018-10-11 06:19
浏览 74
已采纳

如何将搜索表单连接到数据库并在Laravel上检索数据

I am trying to connect my search form to my database and retrieving data from there. But something is going wrong. My controller or my routes... But I couldn't figure it out.

the problem is it's showing the data directly without, searched. I want to see the data when I search. Also try to search and this is coming:

Collection {#221 ▼
  #items: []
}

My view is here:

<form action="{{URL::to('welcome')}}" method="post" role="search" class="searchbox">
    {{csrf_field()}}
    <input type="text" name="q" class="search" placeholder="町, 地域, 会社名, 物件名">
    <input type="submit" name="submit" class="submit" value="search">
  </form>
  @if(isset($details))
      <p> here is the results <b>{{$query}}</b> are : </p>
    @endif
<table cellspacing='0'>
  <thead>
  <tr>
    <th>会社名</th>
    <th>物件名</th>
    <th>住所</th>
    <th>販売価格</th>
    <th>専有面積</th>
    <th>間取り</th>
    <th>竣工時期</th>
    <th>入居時期</th>
  </tr>
  <thead>
  <tbody>
  @foreach($estates as $estate)
    <tr class="even">
      <td>{{$estate->company_name}}</td>
      <td><a href="{{json_decode($estate->link)}}" target="_blank">{{$estate->name}}</a><br/></td>
      <td>{{$estate->address}}</td>
      <td>{{$estate->price}}</td>
      <td>{{$estate->extend}}</td>
      <td>{{$estate->rooms}}</td>
      <td>{{$estate->old}}</td>
      <td>{{$estate->entery}}</td>
    </tr>
  @endforeach
  </tbody>
</table> 

Also here is the controller and my route. What am I doing wring here?

public function welcome()
{

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

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

}

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

        $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);
        }

    }

    return view("welcome")->withMessage("No Found!");
}

Here is route:

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

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

I couldn't figure it out. Also first controller welcome retrieving data smoothly without search form! But I want it to run when I try to search. Any help? Thank you!

  • 写回答

2条回答 默认 最新

  • douwu7168 2018-10-11 06:43
    关注

    Change your form action to

    <form action="{{ route('search.route') }}" method="post" role="search" class="searchbox">
    

    Change your route to

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

    Add use Illuminate\Http\Request; on top of your PagesController and change your controller method to

    public function search(Request $request)
    {
        $q = $request->q;
        if ($q != " "){
    
            $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);
            }
    
        }
    
        return view("welcome")->withMessage("No Found!");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我调试idea基于spring boot项目
  • ¥15 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 分析下图所示同步时序逻辑电路的逻辑功能。
  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合