CityService.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Services;
  3. use App\City;
  4. Class CityService
  5. {
  6. public static $cities = ['汕头','汕尾'];
  7. /*
  8. * array | string $column
  9. * 默认一些select字段,可传递string 或 array来指定select字段
  10. */
  11. public function getSelection($column = ['id','name']){
  12. if (!is_array($column)) {
  13. $column = [$column];
  14. }
  15. return City::query()->select($column)->get();
  16. }
  17. public function find($id){
  18. return City::query()->find($id);
  19. }
  20. public function findByName($name)
  21. {
  22. $city = City::query()->where('name',$name)->first();
  23. if(isset($city))return $city;
  24. foreach (CityService::$cities as $city) {
  25. if(strstr($city,$name) != null){
  26. return City::query()->where('name',$city)->first();
  27. }
  28. }
  29. if(str_ends_with($name,'自治州')){
  30. $city_name = str_split($name,strpos($name,'自治州'))[0];
  31. return City::query()->where('name','like',$city_name.'%')->first();
  32. }
  33. if(str_ends_with($name,'市')){
  34. $city_name = str_split($name,strpos($name,'市'))[0];
  35. return City::query()->where('name','like',$city_name.'%')->first();
  36. }
  37. return null;
  38. }
  39. }