4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LaravelのHasManyを最新の1件をHasOneにする

Last updated at Posted at 2025-03-21

概要

リレーション先はHasManyだが、最新データを1件をリレーションとして処理したい場合に利用できます

テーブル

  • ユーザ情報テーブル
  • 履歴テーブル

ユーザ情報テーブル

ID 名前 メールアドレス パスワード 作成日 更新日
1 山田太郎 yamada@example.com password123 2023-01-01 10:00:00 2023-01-01 10:00:00
2 佐藤花子 sato@example.com password123 2023-01-02 11:00:00 2023-01-02 11:00:00
3 鈴木一郎 suzuki@example.com password123 2023-01-03 12:00:00 2023-01-03 12:00:00
4 高橋次郎 takahashi@example.com password123 2023-01-04 13:00:00 2023-01-04 13:00:00
5 田中三郎 tanaka@example.com password123 2023-01-05 14:00:00 2023-01-05 14:00:00
6 中村四郎 nakamura@example.com password123 2023-01-06 15:00:00 2023-01-06 15:00:00
7 小林五郎 kobayashi@example.com password123 2023-01-07 16:00:00 2023-01-07 16:00:00
8 山本六郎 yamamoto@example.com password123 2023-01-08 17:00:00 2023-01-08 17:00:00
9 井上七郎 inoue@example.com password123 2023-01-09 18:00:00 2023-01-09 18:00:00
10 渡辺八郎 watanabe@example.com 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 山田太郎 yamada@example.com 2023-01-01 2023-01-01 10:00:00 2023-01-01 10:00:00
1 山田太郎 yamada@example.com 2023-01-05 2023-01-05 10:00:00 2023-01-05 10:00:00
2 佐藤花子 sato@example.com 2023-01-02 2023-01-02 11:00:00 2023-01-02 11:00:00
2 佐藤花子 sato@example.com 2023-01-06 2023-01-06 11:00:00 2023-01-06 11:00:00
3 鈴木一郎 suzuki@example.com 2023-01-03 2023-01-03 12:00:00 2023-01-03 12:00:00
3 鈴木一郎 suzuki@example.com 2023-01-07 2023-01-07 12:00:00 2023-01-07 12:00:00
4 高橋次郎 takahashi@example.com 2023-01-04 2023-01-04 13:00:00 2023-01-04 13:00:00
5 田中三郎 tanaka@example.com 2023-01-05 2023-01-05 14:00:00 2023-01-05 14:00:00
6 中村四郎 nakamura@example.com 2023-01-06 2023-01-06 15:00:00 2023-01-06 15:00:00
7 小林五郎 kobayashi@example.com 2023-01-07 2023-01-07 16:00:00 2023-01-07 16:00:00
8 山本六郎 yamamoto@example.com 2023-01-08 2023-01-08 17:00:00 2023-01-08 17:00:00
9 井上七郎 inoue@example.com 2023-01-09 2023-01-09 18:00:00 2023-01-09 18:00:00
10 渡辺八郎 watanabe@example.com 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のデータとして処理することができます。

参考

Laravel11 ドキュメント

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?