我想在我的项目中为纬度和经度添加一个 POINT 字段,我发现我模仿了这个教程以确保它正常工作,但我不知道为什么现在会发生这个错误:
UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "boolean" given.
如果我退出 POINT 字段更改不会发生,我会得到 JSON 数据,允许我使用地址的谷歌地图地理编码而不是纬度和经度数据。
端点操作如下所示,仅在没有 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
]);
}
访问器和修改器与教程中的基本相同,但我将位置字段名称更改为 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);
}
租赁地点的迁移
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' => ''
];
});
我在 RentalLocation 模型中使 latitude_longitude 可填充。
我尝试按照Laracasts 论坛上的建议将所有参数添加到响应中,以防它是 UTF-8 问题,但这并没有改变任何东西:
return response()->json(['data' => $data], 200, [], JSON_UNESCAPED_UNICODE);