PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to get all values from Laravel controller to index using JavaScript?

 April 19, 2022     javascript, laravel, php     No comments   

Issue

I am using Laravel to get all the data from a table and show the list of coordinates for Leaflet. Currently I am working on how to use $gis in the GisController to the index's JavaScript.
So here's the GisController.php

public function index()
{
    $gis = Gis::all();

    return view('gis/map', compact('gis'));
}

and this is from the index.blade.php

foreach ($gis as $gisData) {
    //------- data from controller
    var gisType = $gisData->type;

    if (gisType == 1) {
        //------- data from controller
        var polylinePoints = $gisData->coordinates;
        var polyline = L.polyline(polylinePoints).addTo(map);
    } else if (gisType == 2) {
        //------- data from controller
        var markerPoint= $gisData->coordinates;
        var marker = L.marker(markerPoint).addTo(map);
    }
}

Solution

have 2 ways :

  1. using ajax return object
  2. set variable into javascript
    <script>
    const gisData = <?php echo json_encode($gis); ?>;
    </script>

note : can convert $gis to array



Answered By - Xupitan
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing