Reputation: 95
i'm super new to Laravel and I've been doing some research on how to get the google maps api to work with laravel. I've already made my blade to fetch content from my database with eloquent etc, so I know that wwhen you use blade you can put the vars between two curly brackets to then "echo" them on the webpage (like this {{$training->Location}} )
The problem is that in the following snippet of code I need to pass the data from the Location table into a php var and I don't know how to pass it. That's how I did it , but obviously it didn't work out well , it just passes it as direct text I guess... So when I put this $adress = '{{$training->Location }}; and I echo it on my page for testing purposes I get this string as a result: %7B%7B%24training-%3ELocation%7D%7D which the googleMapsApi will not recognize as a correct adress.
Here's the code snippet: `
<div>
<H4>Location: {!!$training->Location!!}</H4>
</div>
<div>
<H4>Starting Date: {!!$training->DateTime!!} </H4>
</div>
<?php
$adress = '{{$training->Location}}';
//$adress = 'Universitätsring 1, 1010 Wien';
$adress = urlencode($adress);
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $adress . '&sensor=true';
$xml = simplexml_load_file($url);
$status = $xml->status;
if ($status == 'OK') {
$latitude = $xml->result->geometry->location->lat;
$longitude = $xml->result->geometry->location->lng;
} ?>`
I hope you understand my problem , sorry my English isn't the best, but if you don't understand I can always provide screenshots or a better explaination
Upvotes: 1
Views: 2544
Reputation: 234
Although the suggest answer works this is not a good design practice.
In your blade files should not be anny php code other then absolutely necessary for displaying values. The code you wrote in your blade file should be located in the controller. I would suggest you get familiar with the MVC concept Laravel is build on. From the laravel docs (https://laravel.com/docs/5.5/views):
Views contain the HTML served by your application and separate your controller / application logic from your presentation logic.
Making an callout to get the geometric location should be in your controller (or some helper class) at least not in your view. Also this would be a good read: https://scotch.io/@ARKASoftwares/mvc-development-the-need-of-the-changing-world
Upvotes: 1
Reputation: 1416
The simplified workflow is as follows: any Blade code is converted to the equivalent PHP, then the PHP code is executed server-side and the resulting HTML is then passed to the client.
You are using Blade because this is what you want to do (probably because of the much nicer syntax) and certainly not because of some unspoken Laravel constraint. Any variables "available in Blade" are also available in plain PHP for you to use.
Replacing
$address = '{{$training->Location}}';
with
$address = $training->Location;
is perfectly "legal" and should be the way to go if you consider you are in the need for plain PHP.
Upvotes: 0