如何将搜索表单连接到数据库并在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!

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!");
}

here you can do it this way too

change the route like this

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

and change the form action like this

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

or edit you code and pass the url to value in quotes

<form action="{{URL::to('welcome')}}" method="post" role="search" class="searchbox">

For more information check this Laravel Routes