title: Escaping single quote in php laravel
body: <p>I have an address field in the locations database, with a value <code>Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala</code>, the website was working great until I added the location with this address, the google maps plugin no longer works because this single quote has caused a javascript error in the console,</p>
  
  <p><strong>My location controller get function:</strong> </p>
  
  <pre><code>public static function get() {
      $locations = Location::with('country')-&gt;get();
  
      return $locations;
  }
  </code></pre>
  
  <p><strong>My script for loading locations into google maps:</strong></p>
  
  <pre><code>var locations = JSON.parse('{!! $locations !!}');
    var lang = "{{ App::getLocale() }}";
    $(locations).each(function(key, value) {
      allLocationsPins.push({lat: parseInt(value.latitude), lng: parseInt(value.longitude)});
    });
  </code></pre>
  
  <p>The single quote in the address value is causing this error because it's not escaped. I tried adding the <code>JSON encode</code> function to the locations array before returning it from the controller but nothing happened, I think escaping the single quote by <code>Json_encode</code> is not working because of the {!! $locations !!} in the script file, but if I tried to remove the {!!  !!} every character in the string change. </p>
  
  <p>How can I fix this? If <code>json_encode</code> is not working what else can I use ?</p>
  
  <p>I tried to add \ before the single quote in a forloop, but it is not doing anything for some reason, here is the forloop and the output.</p>
  
  <pre><code>public static function get() {
          $locations = Location::with('country')-&gt;get();
  
          foreach ($locations as $location) {
              $location["address"] = str_replace("'","\'",$location["address"]);
          }
          var_dump($locations);
          return $locations;
      }
  
  
  // PART OF VAR DUMP OUTPUT..
  
  array(14) {
          ["id"]=&gt;
          int(7)
          ["title"]=&gt;
          string(6) "Hamala"
          ["title_ar"]=&gt;
          NULL
          ["address"]=&gt;
          string(62) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa\' Complex - Hamala"
          ["address_ar"]=&gt;
          NULL
          ["latitude"]=&gt;
          string(9) "26.166246"
          ["longitude"]=&gt;
          string(9) "50.468510"
          ["phone_numbers"]=&gt;
          string(13) "+973 17610612"
          ["customer_service_email"]=&gt;
          string(28) "Marketing@aljasser-group.net"
          ["working_hours"]=&gt;
          string(84) "From 09:00 AM to 01:00 PM | Evening Shift: From 04:00 PM to 08:00 PM | Friday is off"
          ["working_hours_ar"]=&gt;
          NULL
          ["country_id"]=&gt;
          int(3)
          ["created_at"]=&gt;
          string(19) "2017-11-13 16:21:15"
          ["updated_at"]=&gt;
          string(19) "2017-11-06 19:01:54"
        }
        ["original":protected]=&gt;
        array(14) {
          ["id"]=&gt;
          int(7)
          ["title"]=&gt;
          string(6) "Hamala"
          ["title_ar"]=&gt;
          NULL
          ["address"]=&gt;
          string(61) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala"
          ["address_ar"]=&gt;
          NULL
          ["latitude"]=&gt;
          string(9) "26.166246"
          ["longitude"]=&gt;
          string(9) "50.468510"
          ["phone_numbers"]=&gt;
          string(13) "+973 17610612"
          ["customer_service_email"]=&gt;
          string(28) "Marketing@aljasser-group.net"
          ["working_hours"]=&gt;
          string(84) "From 09:00 AM to 01:00 PM | Evening Shift: From 04:00 PM to 08:00 PM | Friday is off"
          ["working_hours_ar"]=&gt;
          NULL
          ["country_id"]=&gt;
          int(3)
          ["created_at"]=&gt;
          string(19) "2017-11-13 16:21:15"
          ["updated_at"]=&gt;
          string(19) "2017-11-06 19:01:54"
        }
  </code></pre>
  
  <p>As you can see the address location array is found twice the first one which was changed as expected and the second one which is protected and did not change the problem is with is line right here</p>
  
  <pre><code>["address"]=&gt;
          string(61) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala"
  </code></pre>