我想在我的项目中为纬度和经度添加一个POINT字段,我发现了这个教程,我模仿以确保它是工作的,但我被卡在为什么这个错误现在发生:
UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "boolean" given.
如果我退出点字段的变化不发生,我得到的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
]);
}
访问器和mutator本质上与教程相同,但我将位置字段名称更改为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' => ''
];
});
,我使latitude_longitude在RentalLocation模型中可填充。
我尝试按照laracast论坛上的建议将所有参数添加到响应中,以防它是UTF-8问题,但这并没有改变任何东西:
return response()->json(['data' => $data], 200, [], JSON_UNESCAPED_UNICODE);
我想我应该在发表这个答案之前问更多的问题,但我认为你做事情的顺序是错误的。
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;
*/
}
因此,您需要在查询本身中添加groupBy,因为这是SQL应该执行的查询操作。另一部分是,当您将其转换为集合时(这不是100%必需的),您可以返回它。Laravel原生处理JSON