gpt4 book ai didi

php - 由于使用 POINT 数据,Laravel 5 UnexpectedValueException 响应查询

转载 作者:可可西里 更新时间:2023-11-01 09:05:40 25 4
gpt4 key购买 nike

我想在我的项目中为纬度和经度添加一个 POINT 字段,我发现了这个 tutorial我模仿以确保它正常工作,但我仍然坚持为什么现在会发生此错误:

UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "boolean" given.

如果我退出,则 POINT 字段不会发生变化,我得到的 JSON 数据允许我使用地址的 Google map 地理编码而不是纬度和经度数据。

端点操作看起来像这样,它只在没有 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
]);
}

accessor 和 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' => ''
];
});

并且我在 RentalLocation 模型中使 latitude_longitude 可填充。

我尝试按照 Laracasts forum 上的建议将所有参数添加到响应中如果这是一个 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。

关于php - 由于使用 POINT 数据,Laravel 5 UnexpectedValueException 响应查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31436047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com