ReflectionException: Class Chain does not exist
alexc-hollywood opened this issue · 6 comments
General Information
GeocoderLaravel Version: 1.0.1
Laravel Version: 7.4
PHP Version: 7.1
Operating System and Version: Ubuntu 16
Issue Description
Can't reverse geocode because of Chain error.
Steps to Replicate
Function calling the package:
public function reverse_geocode($lat, $lon) {
try {
$result = Geocoder::reverse($lat, $lon);
$data = $result->toArray();
\DB::table('geocoded')->insert([
'type' => 'reverse',
'q' => $lat.','.$lon,
'lat' => $data['latitude'],
'lon' => $data['longitude'],
'coords' => \DB::raw("GeomFromText('POINT(".$data['longitude']." ".$data['latitude'].")')"),
'bounds' => json_encode($data['bounds']),
'street' => $data['streetNumber'] .' '.$data['streetName'],
'zip' => $data['zipcode'],
'city' => $data['city'],
'district' => $data['cityDistrict'],
'county' => $data['county'],
'region' => $data['region'],
'state' => $data['regionCode'],
'country' => $data['countryCode'],
'created_at' => new \Carbon\Carbon,
'updated_at' => new \Carbon\Carbon
]);
return $result;
} catch( \Exception $e ) {
\Log::error($e);
return false;
}
}
Stack Trace
Development.ERROR: ReflectionException: Class Chain does not exist in /srv/Demo.com/dev/vendor/toin0u/geocoder-laravel/src/Providers/GeocoderService.php:71
Stack trace:
#0 /srv/Demo.com/dev/vendor/toin0u/geocoder-laravel/src/Providers/GeocoderService.php(71): ReflectionClass->__construct('Chain')
#1 [internal function]: Geocoder\Laravel\Providers\GeocoderService->Geocoder\Laravel\Providers{closure}(Array, 'Chain')
#2 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Support/Collection.php(684): array_map(Object(Closure), Array, Array)
#3 /srv/Demo.com/dev/vendor/toin0u/geocoder-laravel/src/Providers/GeocoderService.php(78): Illuminate\Support\Collection->map(Object(Closure))
#4 /srv/Demo.com/dev/vendor/toin0u/geocoder-laravel/src/Providers/GeocoderService.php(54): Geocoder\Laravel\Providers\GeocoderService->getProviders(Object(Illuminate\Support\Collection))
#5 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Container/Container.php(716): Geocoder\Laravel\Providers\GeocoderService->Geocoder\Laravel\Providers{closure}(Object(Illuminate\Foundation\Application), Array)
#6 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Container/Container.php(598): Illuminate\Container\Container->build(Object(Closure))
#7 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Container/Container.php(567): Illuminate\Container\Container->resolve('geocoder')
#8 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(702): Illuminate\Container\Container->make('geocoder')
#9 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Container/Container.php(1139): Illuminate\Foundation\Application->make('geocoder')
#10 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(159): Illuminate\Container\Container->offsetGet('geocoder')
#11 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(128): Illuminate\Support\Facades\Facade::resolveFacadeInstance('geocoder')
#12 /srv/Demo.com/dev/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(215): Illuminate\Support\Facades\Facade::getFacadeRoot()
#13 /srv/Demo.com/dev/app/Http/Controllers/Front/BaseController.php(264): Illuminate\Support\Facades\Facade::__callStatic('reverse', Array)
#14 /srv/Demo.com/dev/app/Traits/GeoSearchHelper.php(364): Demo\Http\Controllers\Front\BaseController->reverse_geocode(43.6861, -79.4025)
Thanks for reporting this issue. I will take a look at this and re-run my unit tests to see what's going on.
Your code is not actually performing the geocoding, only preparing it. To run the geocoding, use the get()
method to fetch a collection, or the all()
method to fetch an array, of results. Then iterate of the array to display the results. Your updated code should look like this:
<?php
public function reverse_geocode($lat, $lon) {
try {
$results = app('geocoder')->reverse($lat, $lon)->all();
foreach ($results as $result) {
app('db')->table('geocoded')->insert([
'type' => 'reverse',
'q' => $lat.','.$lon,
'lat' => $data['latitude'],
'lon' => $data['longitude'],
'coords' => \DB::raw("GeomFromText('POINT(".$data['longitude']." ".$data['latitude'].")')"),
'bounds' => json_encode($data['bounds']),
'street' => $data['streetNumber'] .' '.$data['streetName'],
'zip' => $data['zipcode'],
'city' => $data['city'],
'district' => $data['cityDistrict'],
'county' => $data['county'],
'region' => $data['region'],
'state' => $data['regionCode'],
'country' => $data['countryCode'],
'created_at' => new \Carbon\Carbon,
'updated_at' => new \Carbon\Carbon
]);
}
return $result;
} catch (\Exception $e) {
app('log')->error($e);
return false;
}
}
(I like to use the app references instead of Facades, just a preference, not wrong if you use facades.)
This should now run without issue. (Please let me know. I added a unit test for reverse-geocoding and it works.)
As always, if this issue persists, please re-open. :) Good luck!
Thank you for the prompt and detailed response! Legend!
Woot, glad to hear :) Please let me know if you have any feedback for Laravel Geocoder, improvements, or unexpected behavior. Always looking to improve and make it better. :)
Just wanted to update this. The issue was because i was using an older version of the package, and the config file has been updated with the class namespaces. Package update didn't update config.
Ah, that makes sense, since the config file is not automatically republished (and if you're not making any customizations to it, no need to publish it either). Thanks for reporting that back. Will add that as a to-do for documentation.