I have MapController and PagesController. Fetching address row from DB to MapController and passing it to home.view So using it in Google Map. But also in the PagesController I am fetching all the rows including address row too. and passing it on the same view. But when I do that. undefined var error pops up.
Here is controllers and view.
PagesController
public function search(Request $request)
{
$q = $request->q;
$estates = \DB::table('allestates')
->where("building_name", "LIKE", "%" . $q . "%")
->orWhere("address", "LIKE", "%" . $q . "%")
->orWhere("company_name", "LIKE", "%" . $q . "%")
->orWhere("region", "LIKE", "%" . $q . "%")
->orderBy('price')->paginate(5);
return view("home", compact('estates', 'q'));
}
and view
@foreach($estates as $estate)
<div class="row" id="main-wrapper">
<div class="col-sm">
<img src=" https://dummyimage.com/150x150/000/fff" alt="" class="img-radius">
</div>
<div class="col-6">
<h3>{{$estate->building_name}}</h3>
<p>
<br>
{{str_limit($estate->address, $limit = 80)}}
<br>
{{ $estate->price }}
<br>
{{ $estate->rooms }}
<br>
{{ $estate->extend }}
<br>
</p>
</div>
<div class="col-sm">
<button class="detail-btn">物件概要</button>
</div>
<hr style="width: 100%; margin:10px; border-color: #34495e;">
</div>
@endforeach
MapController
$adres = DB::table("allestates")
->get();
return view("home", compact('adres'));
view
function initMap(){
var options = {
zoom:8,
center:
'{!! $adres[0]->address !!}'
}
var map = new google.maps.Map(document.getElementById("map"), options);
var marker = new google.maps.Marker({
position:
@foreach($adres as $addr)
'{!! $addr->address !!}',
@endforeach
map:map
});
var infoWindow = new google.maps.InfoWindow({
content:'content here'
});
marker.addListener('click', function () {
infoWindow.open(map, marker);
})
}
By the way there was no error until I put PagesController in. How can I make this happen? And error showing me the MapController?
Undefined variable: adres (View: /var/www/html/laravel/resources/views/layouts/app.blade.php) (View: /var/www/html/laravel/resources/views/layouts/app.blade.php)
\DB::table('allestates')::all()->get()
. get() is applicable on a collection.->get()
just\DB::table('allestates')::all()
will get all the recordsget($index)
will actually get the record if you need an item. Correct syntax is\DB::table('allestates')->all()
.