Laravel 5 UnexpectedValueException in response to a request due to the use of POINT data

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(); }); // Laravel schema creation doesn't support geographic data - July 2015 Laravel version 5.1 DB::statement('ALTER TABLE rental_locations ADD latitude_longitude POINT'); } 

Seeds for rent.

 protected $rentalLocations = [ [ 'street_address' => '707 Johnson St.', 'postal_code' => 'V8W 1M8', 'latitude_longitude' => '48.4271731,-123.3644049' ], ... ]; public function run() { // Rentals situated within available regions used during development foreach ($this->rentalLocations as $rentalLocation) { // Rental locations used during development factory(App\RentalLocation::class, 1)->create($rentalLocation); } } 

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); 
+3
source share
1 answer

I think I should ask more questions before posting this answer, but I think you are doing something in the wrong order.

 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') ->select('*') ->where('rentals.region_id', '=', $id) ->groupBy('rental_location_id') ->get(); return collect($rentals); // or return $rentals /* Not necessary // Create a collection from the array of query results $rentals = collect($rentals); // Laravel is set up to return collections as json when directly returned return $rentals; */ } 

So, you need to add groupBy in the query itself, because this is the query action that your SQL should execute. The other part is that when you convert it to a collection (which is not 100% necessary), you can simply return it. Laravel handles JSON natively.

0
source

Source: https://habr.com/ru/post/1244067/


All Articles