*/ private array $countryIdTables = [ 'companies' => 'Companies', 'warehouses' => 'Warehouses', 'users' => 'Users', 'suppliers' => 'Suppliers', 'customers' => 'Customers', 'carriers' => 'Carriers', 'addresses' => 'Addresses', 'billing_addresses' => 'Billing addresses', 'shipping_addresses' => 'Shipping addresses', ]; /** * Tables commonly storing ISO alpha-2 country codes. * * @var array */ private array $alpha2Tables = [ 'companies' => 'Companies by ISO code', 'warehouses' => 'Warehouses by ISO code', 'addresses' => 'Addresses by ISO code', 'billing_addresses' => 'Billing addresses by ISO code', 'shipping_addresses' => 'Shipping addresses by ISO code', ]; /** * Return usage counts without failing when optional business tables do not exist. * * @return array */ public function forCountry(Country $country): array { $rows = []; foreach ($this->countryIdTables as $table => $label) { if (! Schema::hasTable($table) || ! Schema::hasColumn($table, 'country_id')) { continue; } $rows[] = [ 'label' => $label, 'table' => $table, 'column' => 'country_id', 'count' => (int) DB::table($table)->where('country_id', $country->id)->count(), ]; } foreach ($this->alpha2Tables as $table => $label) { foreach (['country_code', 'country_alpha2', 'iso_country_code'] as $column) { if (! Schema::hasTable($table) || ! Schema::hasColumn($table, $column)) { continue; } $rows[] = [ 'label' => $label, 'table' => $table, 'column' => $column, 'count' => (int) DB::table($table)->where($column, $country->alpha2)->count(), ]; } } return $rows; } }