Laravel 5 UnexpectedValueException由于使用POINT数据而在查询响应中 [英] Laravel 5 UnexpectedValueException in response of query due to use of POINT data

查看:235
本文介绍了Laravel 5 UnexpectedValueException由于使用POINT数据而在查询响应中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中添加一个经度和纬度的POINT字段,我发现了这个教程以确保它可以正常工作,但是我一直坚持为什么现在会出现此错误:

I wanted to add a POINT field for latitude and longitude in my project and I found this tutorial that I mimicked to make sure it was working, but I'm stuck on why this error is occurring now:

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

如果我退出POINT字段,则不会发生更改,并且会获得JSON数据,该数据使我可以使用地址的Google Maps Geocoding代替纬度和经度数据.

Which if I back out the POINT field changes doesn't occur and I get the JSON data that allows me to use a Google Maps Geocoding of the address instead of the latitude and longitude data.

终结点操作如下所示,仅在没有POINT数据的情况下起作用:

The endpoint action looks like this, which only works without the POINT data:

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:

The accessor and mutator are essentially the same from the tutorial, but I changed the location field name to 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模型中填写了纬度.

and I made latitude_longitude fillable in the RentalLocation Model.

我尝试按照

I tried adding all the parameters to the response as suggested on Laracasts forum in case it was a UTF-8 issue, but that didn't change anything:

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

推荐答案

我认为在发布此答案之前,我应该问更多的问题,但是我认为您做事的顺序不正确.

I think I should ask more questions before I post this answer, but I think you're doing things in the wrong order.

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.

So you need to add your groupBy in the query itself because that is a query action that your SQL should be doing. The other part is that when you convert it to a collection (which isn't 100% necessary) you can just return it. Laravel handles the JSON natively.

这篇关于Laravel 5 UnexpectedValueException由于使用POINT数据而在查询响应中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆