0

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)

4
  • try with \DB::table('allestates')::all()->get(). get() is applicable on a collection. Commented Dec 4, 2018 at 6:10
  • @FarooqKhan You don't need ->get() just \DB::table('allestates')::all() will get all the records
    – Josh Young
    Commented Dec 4, 2018 at 6:13
  • ^ agreed. get($index) will actually get the record if you need an item. Correct syntax is \DB::table('allestates')->all(). Commented Dec 4, 2018 at 6:15
  • this give me this error: Method Illuminate\Database\Query\Builder::all does not exist.
    – firefly
    Commented Dec 4, 2018 at 6:17

3 Answers 3

1

Thank you for answering me, but I solved the problem with little touch. Just gave the same $var in two controllers.

0

instead of that @foreach inside js adecuate this to your code:

var data = {!! $addres !!};
debugger; < --- use this to inspect that the array exists

then make a for iteration and place a marker for every item in this way (changing the variables that you have different in your database):

for (var i = 0; i < data.length; i++) {
var pos = new google.maps.LatLng(data[i].latitud, data[i].longitud);

markers[i] = new google.maps.Marker({
    position: pos,
    map: map,
    icon: 'images/locred.png',
    description: data[i].desc,
    id: i
});

var infowindow = new google.maps.InfoWindow({
    content: data[i].name
});

infowindow.open(map, markers[i]);
google.maps.event.addListener(markers[i], 'click', function () {
    alert(markers[this.id].description)
})

}

0

In you MapsController, try this

$adres = DB::table("allestates")->all();
return view("home", compact('adres'));
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.