概要
ユーザー登録が必要なサービスを開発しているとき、ユーザー登録後にデフォルトで初期アイコンを設定する方法をメモします。
方法
1.デフォルトで設定したい画像を任意の場所に保存(今回はApp\Storage\app\public)
2.ユーザー登録時にコントローラからデフォルト画像をデータベースに登録する
なお、Laravel sanctum
を使ったSPAユーザー認証を採用しています。
データベース
mysql> desc users;
+-------------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-----------------+------+-----+---------+----------------+
| id | bigint unsigned | NO | PRI | NULL | auto_increment |
| login_id | varchar(255) | NO | UNI | NULL | |
| name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| email_verified_at | timestamp | YES | | NULL | |
| password | varchar(255) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-------------------+-----------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)
mysql> desc profiles;
+-------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------------+------+-----+---------+----------------+
| profiles_id | bigint unsigned | NO | PRI | NULL | auto_increment |
| user_id | bigint unsigned | NO | MUL | NULL | |
| icon_title | varchar(255) | YES | | NULL | |
| icon_path | varchar(255) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-------------+-----------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
コントローラ
RegisterController.php
<?php
namespace App\Http\Controllers\Auth\Api;
use App\Http\Controllers\Controller;
use App\Models\Profile;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class RegisterController extends Controller
{
use RegistersUsers;
public function register(Request $request)
{
// リクエストを検証
$this->validator($request->all())->validate();
// ユーザーとトークンの作成
$data = DB::transaction(function () use ($request) {
$user = $this->create($request->all());
$token = $user->createToken($request->device_name ?? 'undefined')->plainTextToken;
//↓ここを追加
//保存した初期アイコンのパスごとデータベースに登録
$profile = Profile::create([
'user_id' => $user->id,
'icon_title' => 'default_icon.jpg',
'icon_path' => 'storage/' . 'default_icon.jpg'
]);
return json_encode(['token' => $token, 'user' => $user, 'profile' => $profile]);
});
// userとtokenのjsonを返却
return response($data);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'login_id' => ['required', 'string', 'max:255', 'unique:users'],
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'login_id' => $data['login_id'],
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
まとめ
これで初期アイコンを設定することができました。