Я хотел добавить в свой проект поле POINT для широты и долготы и нашел это учебник, который я сымитировал, чтобы убедиться, что он работает, но я застрял на том, почему эта ошибка возникает сейчас:
UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "boolean" given.
Что, если я откажусь от изменений в поле POINT, не произойдет, и я получу данные JSON, которые позволят мне использовать геокодирование адреса Google Maps вместо данных широты и долготы.
Действие конечной точки выглядит следующим образом, оно работает только без данных POINT:
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
]);
}
Аксессор и мутатор по сути такие же, как в учебнике, но я изменил имя поля местоположения на широта_долгота:
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);
}
Миграция для пунктов проката
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's schema creation doesn't support geographic data - July 2015 Laravel version 5.1
DB::statement('ALTER TABLE rental_locations ADD latitude_longitude POINT');
}
Исходное число для пунктов проката
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->define(Parkable\RentalLocation::class, function ($faker) {
return [
'street_address' => '',
'city' => 'Victoria',
'province' => 'British Columbia',
'country' => 'Canada',
'postal_code' => '',
'latitude_longitude' => ''
];
});
и я сделал latitude_longitude заполняемым в модели RentalLocation.
Я попытался добавить все параметры в ответ, как было предложено на Форум Laracasts на случай, если проблема была в UTF-8, но это ничего не изменило:
return response()->json(['data' => $data], 200, [], JSON_UNESCAPED_UNICODE);