| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace App\Services;
- use App\City;
- Class CityService
- {
- public static $cities = ['汕头','汕尾'];
- /*
- * array | string $column
- * 默认一些select字段,可传递string 或 array来指定select字段
- */
- public function getSelection($column = ['id','name']){
- if (!is_array($column)) {
- $column = [$column];
- }
- return City::query()->select($column)->get();
- }
- public function find($id){
- return City::query()->find($id);
- }
- public function findByName($name)
- {
- $city = City::query()->where('name',$name)->first();
- if(isset($city))return $city;
- foreach (CityService::$cities as $city) {
- if(strstr($city,$name) != null){
- return City::query()->where('name',$city)->first();
- }
- }
- if(str_ends_with($name,'自治州')){
- $city_name = str_split($name,strpos($name,'自治州'))[0];
- return City::query()->where('name','like',$city_name.'%')->first();
- }
- if(str_ends_with($name,'市')){
- $city_name = str_split($name,strpos($name,'市'))[0];
- return City::query()->where('name','like',$city_name.'%')->first();
- }
- return null;
- }
- }
|