由于使用了POINT数据

时间:2015-07-15 16:31:04

标签: php mysql laravel laravel-5 laravel-5.1

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

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

如果我退出POINT字段,那么更改并不会发生,我会获得允许我使用地图的Google地图地理编码而不是纬度和经度数据的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 forum的建议将所有参数添加到响应中,以防它出现UTF-8问题,但这并没有改变任何内容:

return response()->json(['data' => $data], 200, [], JSON_UNESCAPED_UNICODE);

1 个答案:

答案 0 :(得分:0)

我想在发布这个答案之前我应该​​提出更多问题,但我认为你的做法是错误的。

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。