Php 由于使用了点数据,Laravel 5在响应查询时出现意外值异常

Php 由于使用了点数据,Laravel 5在响应查询时出现意外值异常,php,mysql,laravel,laravel-5,laravel-5.1,Php,Mysql,Laravel,Laravel 5,Laravel 5.1,我想在我的项目中为纬度和经度添加一个点字段,我发现了这个字段,我模仿了它以确保它正常工作,但我仍然无法解释为什么现在会出现这个错误: UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(), "boolean" given. 如果我回退点字段,就不会发生更改,并且我得到了JSON数据,它允许我使用Goo

我想在我的项目中为纬度和经度添加一个点字段,我发现了这个字段,我模仿了它以确保它正常工作,但我仍然无法解释为什么现在会出现这个错误:

UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "boolean" given.
如果我回退点字段,就不会发生更改,并且我得到了JSON数据,它允许我使用Google Maps对地址进行地理编码,而不是纬度和经度数据

端点操作如下所示,仅在没有点数据的情况下有效:

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\u-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' => ''
    ];
});
租用地点的种子

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' => ''
    ];
});
工厂出租地点

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' => ''
    ];
});
我让纬度和经度可以在租金分配模型中填充

我尝试按照上的建议将所有参数添加到响应中,以防出现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。

@mtpultz我查看了教程,所以我不知道您正试图使用该点的哪一部分,或者这与该点有什么关系,但我给出的应该可以让我知道!!嗨,这似乎抛出了相同的错误。除了更改字段名之外,我将实现保持原样,并且没有使用scopeDistance操作。基本上,我想使用点数据,并让mutator和accessor处理格式化。所以我把lat和long作为“lat,long”传递,它转换成POINT,反之亦然。如果我只返回我试图添加的字段,所有内容都有效。你能告诉我更多关于哪个部分返回错误的信息吗?我们100%确信rentals函数返回的是布尔值?您还可以使用Homestead 2附带的mysql 5.6.19验证您使用的mysql版本吗。我向我的朋友发出一声宁静的呼唤RentalController@rentals获取一个区域内的所有租金以及该租金的位置数据。在将本教程添加到我的模型中之前,这返回了正确的信息,我必须使用Google的地理编码器通过传回的地址信息获取位置,但我想使用纬度和经度作为点数据,因为我可以在不使用地理编码器的情况下制作Google地图标记。现在,完全相同的操作返回此错误。第403行的响应是什么。请添加到您的问题。。。返回的函数最好是:D@mtpultz你想出解决这个问题的办法了吗?我也被困在这个问题上。当我添加有点空间键要连接的表时,我会得到这个错误。有什么建议吗?嗨,Sadee,我没能解决这个问题。我最终只是使用谷歌地图地理编码,而不是lat和long。这太令人沮丧了。