I wanted to add a POINT field for latitude and longitude in my project, and I found this tutorial that I simulated to make sure this worked, but I was fixated on why this error is happening now:
UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(), "boolean" given.
What if I refuse to change the POINT field, it doesnβt happen, and I get JSON data that allows me to use Google Maps geocoding for the address instead of latitude and longitude data.
The endpoint action looks like it only works without POINT data:
public function rentals($id) { // Retrieve all rentals within a region and the locations spatial data $rentals = DB::table('rentals') ->join('regions', 'rentals.region_id', '=', 'regions.id') ->join('rental_locations', 'rentals.rental_location_id', '=', 'rental_locations.id') ->where('rentals.region_id', '=', $id) ->select('*') ->get(); // Create a collection from the array of query results $rentals = collect($rentals); // Group all rentals by location $rentals = $rentals->groupBy('rental_location_id'); $data = [ 'rentals' => $rentals ]; return response()->json([ 'data' => $data ]); }
The accessor and the mutator are basically the same as the tutorial, but I changed the name of the location field to latitude_longitude:
public function setLatitudeLongitudeAttribute($value) { $this->attributes[ 'latitude_longitude' ] = DB::raw("POINT($value)"); } public function getLatitudeLongitudeAttribute($value) { $location = substr($value, 6); $location = preg_replace('/[ ,]+/', ',', $location, 1); return substr($location, 0, -1); }
Migration for rent.
public function up() { Schema::create('rental_locations', function (Blueprint $table) { $table->increments('id'); $table->string('street_address', 100); $table->string('city', 50); $table->string('province', 50); $table->string('country', 50); $table->string('postal_code', 10); $table->timestamps(); });
Seeds for rent.
protected $rentalLocations = [ [ 'street_address' => '707 Johnson St.', 'postal_code' => 'V8W 1M8', 'latitude_longitude' => '48.4271731,-123.3644049' ], ... ]; public function run() {
Factory for rental locations
$factory->define(Parkable\RentalLocation::class, function ($faker) { return [ 'street_address' => '', 'city' => 'Victoria', 'province' => 'British Columbia', 'country' => 'Canada', 'postal_code' => '', 'latitude_longitude' => '' ]; });
and I made latitude_longitude populated in the RentalLocation model.
I tried to add all the parameters to the answer, as suggested on the Laracasts forum , if this was a UTF-8 problem, but it isnβt, t change something:
return response()->json(['data' => $data], 200, [], JSON_UNESCAPED_UNICODE);