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!