IT & Programming

Build leaflet map with Laravel framework

We will be using leaflet, mapbox and laravel to generate a map. It should be noted that map only accepts coordinates in form of latitude and longitude. You must first import latitude and longitude in your local mysql database to proceed. Please read this blog to import coordinates.

Database Table

Please make a table with name "addresses" and a model name "Address". The "Addresses" table must have following fields.

address (varchar 255)
latitude (double)
longitude (double)

Once you created the table and model. Add some addresses with latitude and longitude into it.


The index function of controller fetches data from "addresses" table and stores into locations array. The first element of the location array "location" is the tool tip view, second and third elements are latitude and longitude.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Address;

class MapController extends Controller
    public function index()
        $locations = [];     

        foreach (Address::All() as $address) {

            $locations[] = [
                     'location' => view('map-tool-tip')->with(['address' => $address])->render(), 
                     'latitude' => $address->latitude, 
                     'longitude' => $address->longitude

        return view('map')->with([
                    'locations' => json_encode($locations)


This blade file is a tool tip view of the address that we will be visible when the marker of the map is clicked. 

  {{ $address->address }}


Finally, the main map blade file where we send location data in json format from AddressController's index function. Don't forget to update default zoom location at .setView([47.977894, -96.569046], 12, [50, 50]); which includes default coordinates of Belgium.

<!DOCTYPE html>
<html lang="en">
        <meta charset="utf-8">
        <link href="" rel="stylesheet">
        <link rel="stylesheet" href="">
        <link rel="stylesheet" href="">
        <script src=""></script>
        <script src=""></script>
        <script src=""></script>
        <script src=""></script>
        <style type="text/css">
            #map {
                width: 600px;
                height: 400px;

        <div id="map"></div>
        <script type="text/javascript">
            var mapboxTiles = L.tileLayer('http://{s}{z}/{x}/{y}.png', {
                attribution: '<a target="_blank" href="" target="_blank">@ Mapbox</a>'

            var map ='map')
                    .setView([47.977894, -96.569046], 12, [50, 50]);

            $.each({!! $locations !!}, function (key, val) {
                popup = L.popup({
                    closeOnClick: true,
                    autoClose: true

                L.marker([val.latitude, val.longitude]).addTo(map).bindPopup(popup);

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *