Edit File: CountryService.php
<?php namespace App\Services; use App\Models\Country; use App\Models\{Delegate, User, Provider}; class CountryService extends BaseService { public function __construct() { parent::__construct(Country::class); } public function getFlags() { $flags = []; foreach (\File::files(public_path('admin/assets/flags/png')) as $path) { $file = pathinfo($path); $flags[] = $file['filename'] . '.' . $file['extension']; } return $flags; } public function getAllCountries() { return $this->model::all(); } public function getCountriesWithCity() { return $this->model::with('cities')->latest()->get(); } public function delete($id) { $country = $this->model::findOrFail($id); $users = User::where('country_code', 'LIKE', '%' . fixPhone($country->key) . '%')->exists(); $providers = Provider::where('country_code', 'LIKE', '%' . fixPhone($country->key) . '%')->exists(); $delegates = Delegate::where('country_code', 'LIKE', '%' . fixPhone($country->key) . '%')->exists(); $city = $country->cities()->exists(); if ($users || $providers || $delegates) { return ['key' => 'error', 'msg' => __('admin.country_related_with_users')]; } if ($city) { return ['key' => 'error', 'msg' => __('admin.country_related_with_cities')]; } $country->delete(); return ['key' => 'success', 'msg' => __('admin.deleted_successfully')]; } public function deleteAll($request): array { $requestIds = json_decode($request['data']); $has_users = false; $has_cities = false; foreach (array_column($requestIds, 'id') as $id) { $country = $this->model::findOrFail($id); $users = User::where('country_code', 'LIKE', '%' . fixPhone($country->key) . '%')->exists(); $providers = Provider::where('country_code', 'LIKE', '%' . fixPhone($country->key) . '%')->exists(); $delegates = Delegate::where('country_code', 'LIKE', '%' . fixPhone($country->key) . '%')->exists(); $city = $country->cities()->exists(); if ($city) { $has_cities = true; break; } if ($users || $providers || $delegates) { $has_users = true; break; } else { $country->delete(); } } return [ 'key' => $has_users ? 'error' : 'success', 'msg' => !$has_users && !$has_cities ? __('admin.deleted_successfully') : __('admin.country_related_with_users_or_cities') ]; } }
Back to File Manager