1

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

2
  • 2
    Just remove the blade syntax. It will work fine as a normal PHP variable. $adress = $training->Location; Commented Dec 1, 2017 at 14:08
  • works like a charm thanks for the super fast reply, I learned something today :) Thanks again Commented Dec 1, 2017 at 14:13

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

Thanks , now I've got clarified on that subject! Appreciate your elaborated answer
@AbdelKdj i would not recommend using php in your blade files. Why would you go against the core design practice of blade. I would suggest stop using blade and using php files instead or move your code to the appropriate place (Controller) see my answer for some explanation on why it should be in the controller.
@AbdelKdj I completely agree, from a software design perspective, that PHP block should definitely not be inside a Blade file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.