ダミーデータの日本語化する方法
解決したいこと
ダミーデータに日本語を入れると文字化けしてしまいます。
config/app.phpで'faker_locale' => 'ja_JP',
に変更したのですが適応されません。
他に原因が思いつかないのでご助言いただきたいです。
発生している問題・エラー
mysql> select * from departments;
+----+---------------------------+--------------+---------------------+---------------------+------------+
| id | name | manager_name | created_at | updated_at | deleted_at |
+----+---------------------------+--------------+---------------------+---------------------+------------+
| 1 | Amari Kassulke Jr. | ??12 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 2 | Prof. Serenity Bartell | ??5 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 3 | Alessandra Wunsch | ??8 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 4 | Roberta Adams | ??16 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 5 | Geovanny Ortiz | ??11 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 6 | Lauren Conroy | ??16 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 7 | Prof. Zane Balistreri Sr. | ??16 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 8 | Kathlyn Hessel II | ??1 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 9 | Janiya Torphy | ??9 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 10 | Mervin Schmeler | ??6 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 11 | Sydni Schamberger DDS | ??18 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 12 | Ms. Heaven Wiza IV | ??19 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 13 | Julius Kemmer | ??6 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 14 | Yasmine Torphy | ??11 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 15 | Prof. Marquis Ledner | ??11 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 16 | Hunter Wintheiser | ??8 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 17 | Owen Ebert | ??1 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 18 | Mr. Johnpaul Ullrich PhD | ??11 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 19 | Jordy Ward | ??17 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
| 20 | Terrence Carter | ??7 | 2024-03-10 08:16:54 | 2024-03-10 08:16:54 | NULL |
+----+---------------------------+--------------+---------------------+---------------------+------------+
20 rows in set (0.00 sec)
- nameが日本語になっていない
- manager_nameに設定した日本語が「??」に変換されている。
該当するソースコード
config/app.phpファイルで日本語設定に変更しました。
app.php
// 'faker_locale' => 'en_US',
'faker_locale' => 'ja_JP',
DepartmentFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class DepartmentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$departmentNum = $this->faker->numberBetween(1,20);
return [
'name' => $this->faker->name(),
'manager_name' => "部署{$departmentNum}",
];
}
/**
* Indicate that the model's email address should be unverified.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function unverified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
}
DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\Department::factory(20)->create();
}
}
自分で試したこと
app.phpファイルで日本語設定に変更
先述の通り、app.phpファイルで日本語設定に変更しました。
文字コードをutf8mb4に変更
mysql> show variables like "chara%";
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.02 sec)
mysql> SET NAMES utf8mb4;
Query OK, 0 rows affected (0.01 sec)
mysql> show variables like "chara%";
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
その他確認が必要な点があれば教えていただきたいです。
よろしくお願いいたします。
0