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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么