2025.03.21
LaravelのHasManyを最新の1件をHasOneにする
池田
Laravel
概要
リレーション先はHasManyだが、最新データを1件をリレーションとして処理したい場合に利用できます
テーブル
- ユーザ情報テーブル
- 履歴テーブル
ユーザ情報テーブル
ID 名前 メールアドレス パスワード 作成日 更新日 1 山田太郎 [[email protected]](mailto:[email protected]) password123 2023-01-01 10:00:00 2023-01-01 10:00:00 2 佐藤花子 [[email protected]](mailto:[email protected]) password123 2023-01-02 11:00:00 2023-01-02 11:00:00 3 鈴木一郎 [[email protected]](mailto:[email protected]) password123 2023-01-03 12:00:00 2023-01-03 12:00:00 4 高橋次郎 [[email protected]](mailto:[email protected]) password123 2023-01-04 13:00:00 2023-01-04 13:00:00 5 田中三郎 [[email protected]](mailto:[email protected]) password123 2023-01-05 14:00:00 2023-01-05 14:00:00 6 中村四郎 [[email protected]](mailto:[email protected]) password123 2023-01-06 15:00:00 2023-01-06 15:00:00 7 小林五郎 [[email protected]](mailto:[email protected]) password123 2023-01-07 16:00:00 2023-01-07 16:00:00 8 山本六郎 [[email protected]](mailto:[email protected]) password123 2023-01-08 17:00:00 2023-01-08 17:00:00 9 井上七郎 [[email protected]](mailto:[email protected]) password123 2023-01-09 18:00:00 2023-01-09 18:00:00 10 渡辺八郎 [[email protected]](mailto:[email protected]) password123 2023-01-10 19:00:00 2023-01-10 19:00:00 履歴テーブル
ユーザID (user_id) 名前 (name) メールアドレス (email) 実行日 (execution_date) 作成日 (created_at) 更新日 (updated_at) 1 山田太郎 [[email protected]](mailto:[email protected]) 2023-01-01 2023-01-01 10:00:00 2023-01-01 10:00:00 1 山田太郎 [[email protected]](mailto:[email protected]) 2023-01-05 2023-01-05 10:00:00 2023-01-05 10:00:00 2 佐藤花子 [[email protected]](mailto:[email protected]) 2023-01-02 2023-01-02 11:00:00 2023-01-02 11:00:00 2 佐藤花子 [[email protected]](mailto:[email protected]) 2023-01-06 2023-01-06 11:00:00 2023-01-06 11:00:00 3 鈴木一郎 [[email protected]](mailto:[email protected]) 2023-01-03 2023-01-03 12:00:00 2023-01-03 12:00:00 3 鈴木一郎 [[email protected]](mailto:[email protected]) 2023-01-07 2023-01-07 12:00:00 2023-01-07 12:00:00 4 高橋次郎 [[email protected]](mailto:[email protected]) 2023-01-04 2023-01-04 13:00:00 2023-01-04 13:00:00 5 田中三郎 [[email protected]](mailto:[email protected]) 2023-01-05 2023-01-05 14:00:00 2023-01-05 14:00:00 6 中村四郎 [[email protected]](mailto:[email protected]) 2023-01-06 2023-01-06 15:00:00 2023-01-06 15:00:00 7 小林五郎 [[email protected]](mailto:[email protected]) 2023-01-07 2023-01-07 16:00:00 2023-01-07 16:00:00 8 山本六郎 [[email protected]](mailto:[email protected]) 2023-01-08 2023-01-08 17:00:00 2023-01-08 17:00:00 9 井上七郎 [[email protected]](mailto:[email protected]) 2023-01-09 2023-01-09 18:00:00 2023-01-09 18:00:00 10 渡辺八郎 [[email protected]](mailto:[email protected]) 2023-01-10 2023-01-10 19:00:00 2023-01-10 19:00:00 HasOneにする処理
User.php
/**
* @return HasOne
*/
public function userLog(): HasOne
{
return $this->hasOne(related: UserLog::class, foreignKey: 'user_id', localKey: 'id')
->latestOfMany(column: 'execution_date');
}例) 山田太郎の最新の履歴は「2023-01-05」のデータが取得される
最も古いデータにする場合
User.php
/**
* @return HasOne
*/
public function userLog(): HasOne
{
return $this->hasOne(related: UserLog::class, foreignKey: 'user_id', localKey: 'id')
->oldestOfMany(column: 'execution_date');
}まとめ
履歴などの1対多のデータのなかで最新、一番古いデータのみ欲しい場合に「latestOfMany」を利用することで、
1対多を1対1のデータとして処理することができます。


