質問編集履歴
3
書式追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -90,8 +90,111 @@
|
|
90
90
|
$response->assertStatus(200);
|
91
91
|
}
|
92
92
|
}
|
93
|
-
|
94
|
-
```
|
93
|
+
```
|
94
|
+
#### モデル
|
95
|
+
```PHP
|
96
|
+
<?php
|
97
|
+
|
98
|
+
namespace App;
|
99
|
+
|
100
|
+
use Illuminate\Database\Eloquent\Model;
|
101
|
+
use Illuminate\Support\Facades\DB;
|
102
|
+
|
103
|
+
/**
|
104
|
+
* 市区町村一覧
|
105
|
+
*/
|
106
|
+
class CityInfo extends Model
|
107
|
+
{
|
108
|
+
protected $fillable = [
|
109
|
+
'id', // 都道府県id+市区町村id
|
110
|
+
'prefecture_id', // 都道府県id
|
111
|
+
'prefecture', // 都道府県
|
112
|
+
'city_id', // 市区町村id
|
113
|
+
'prefecture_name', // 都道府県名
|
114
|
+
'city_name', // 市区町村名
|
115
|
+
'prefecture_name_rome', // 都道府県名ローマ字
|
116
|
+
'city_name_rome', // 市区町村名ローマ字
|
117
|
+
'updated_at', // 更新日時
|
118
|
+
];
|
119
|
+
|
120
|
+
public static function getCitylist()
|
121
|
+
{
|
122
|
+
$city_list = DB::table('city_infos')
|
123
|
+
->select(
|
124
|
+
'city_name',
|
125
|
+
'id',
|
126
|
+
'prefecture_id',
|
127
|
+
'city_name',
|
128
|
+
'city_name_rome'
|
129
|
+
)
|
130
|
+
->orderBy('city_infos.id', 'asc');
|
131
|
+
|
132
|
+
return $city_list;
|
133
|
+
}
|
134
|
+
|
135
|
+
public static function getCityname()
|
136
|
+
{
|
137
|
+
$city_name = DB::table('city_infos')
|
138
|
+
->select('city_name')
|
139
|
+
->orderBy('city_infos.id', 'asc');
|
140
|
+
|
141
|
+
return $city_name;
|
142
|
+
}
|
143
|
+
|
144
|
+
public static function getPagetitleCityNames($url_parameter_city_names)
|
145
|
+
{
|
146
|
+
$pagetitle_city_names = '';
|
147
|
+
$city_names = explode('_', $url_parameter_city_names);
|
148
|
+
$city_names = DB::table('city_infos')
|
149
|
+
->select('city_name')
|
150
|
+
->whereIn('city_name_rome', $city_names)
|
151
|
+
->get()->values()->all();
|
152
|
+
foreach ($city_names as $city_name) {
|
153
|
+
$pagetitle_city_names .= '/' .$city_name->city_name;
|
154
|
+
}
|
155
|
+
return $pagetitle_city_names;
|
156
|
+
}
|
157
|
+
|
158
|
+
public static function getIdAndCityNameAndCityNameRomeWhereByPrefectureId(int $id)
|
159
|
+
{
|
160
|
+
return self::select(
|
161
|
+
'id',
|
162
|
+
'city_name',
|
163
|
+
'city_name_rome'
|
164
|
+
)
|
165
|
+
->where('prefecture_id', $id)
|
166
|
+
->get();
|
167
|
+
}
|
168
|
+
|
169
|
+
public static function getIdWherePrefectureIdAndWhereCityNameRomeLikeFirst(int $prefecture_id, string $city_name)
|
170
|
+
{
|
171
|
+
return self::select('id')
|
172
|
+
->where('prefecture_id', $prefecture_id)
|
173
|
+
->where('city_name_rome', 'like', $city_name . '%')
|
174
|
+
->first();
|
175
|
+
}
|
176
|
+
|
177
|
+
public static function getIdWherePrefectureId(int $id)
|
178
|
+
{
|
179
|
+
return self::select('id')
|
180
|
+
->where('prefecture_id', $id)
|
181
|
+
->get();
|
182
|
+
}
|
183
|
+
|
184
|
+
/**
|
185
|
+
* @param int $prefecture_id
|
186
|
+
*
|
187
|
+
* @return object
|
188
|
+
*/
|
189
|
+
public static function wherePrefectureId(int $prefecture_id)
|
190
|
+
{
|
191
|
+
return self::where('prefecture_id', $prefecture_id)
|
192
|
+
->get();
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
```
|
197
|
+
|
95
198
|
#### マイグレーションファイル
|
96
199
|
```PHP
|
97
200
|
<?php
|
2
文法の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
```
|
17
17
|
|
18
18
|
### 該当のソースコード
|
19
|
-
|
19
|
+
#### テストコード
|
20
20
|
```PHP
|
21
21
|
<?php
|
22
22
|
|
@@ -92,6 +92,46 @@
|
|
92
92
|
}
|
93
93
|
|
94
94
|
```
|
95
|
+
#### マイグレーションファイル
|
96
|
+
```PHP
|
97
|
+
<?php
|
98
|
+
|
99
|
+
use Illuminate\Database\Migrations\Migration;
|
100
|
+
use Illuminate\Database\Schema\Blueprint;
|
101
|
+
|
102
|
+
class CreateCityInfosTable extends Migration
|
103
|
+
{
|
104
|
+
/**
|
105
|
+
* Run the migrations.
|
106
|
+
*
|
107
|
+
* @return void
|
108
|
+
*/
|
109
|
+
public function up()
|
110
|
+
{
|
111
|
+
Schema::create('city_infos', function (Blueprint $table) {
|
112
|
+
$table->integer('id')->unsigned()->primary();
|
113
|
+
$table->boolean('prefecture_id')->index('prefecture_id_idx')->comment('都道府県ID');
|
114
|
+
$table->integer('city_cd')->unsigned()->comment('市区町村コード');
|
115
|
+
$table->string('prefecture_name')->comment('都道府県名');
|
116
|
+
$table->string('city_name')->comment('市区町村名');
|
117
|
+
$table->string('prefecture_name_rome')->comment('都道府県名 ローマ字表記');
|
118
|
+
$table->string('city_name_rome')->comment('市区町村名 ローマ字表記');
|
119
|
+
$table->timestamps();
|
120
|
+
});
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
/**
|
125
|
+
* Reverse the migrations.
|
126
|
+
*
|
127
|
+
* @return void
|
128
|
+
*/
|
129
|
+
public function down()
|
130
|
+
{
|
131
|
+
Schema::drop('city_infos');
|
132
|
+
}
|
133
|
+
}
|
134
|
+
```
|
95
135
|
|
96
136
|
### 試したこと
|
97
137
|
|
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -96,7 +96,8 @@
|
|
96
96
|
### 試したこと
|
97
97
|
|
98
98
|
主キー制約の重複によって引き起こされているので、CityInfoFactoryクラスのid(主キー)にuniqueメソッドを追加。
|
99
|
-
```
|
99
|
+
```PHP
|
100
|
+
|
100
101
|
<?php
|
101
102
|
|
102
103
|
namespace Database\Factories;
|