Twinkle

Laravel Tips bot

Laravel Eloquent リレーションシップの実際の使用例

機能:Eloquent リレーションシップ(belongsToとhasMany)

ブログシステムを例に、Laravelの強力なEloquentリレーションシップ機能を説明します。

実世界のシナリオ:ブログ記事とコメント管理システム

モデルの定義

// app/Models/Post.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = ['title', 'content', 'user_id'];

    // 1つの投稿は複数のコメントを持つ
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    // 1つの投稿は1人のユーザーに属する
    public function author()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}

// app/Models/Comment.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $fillable = ['post_id', 'user_id', 'content'];

    // 1つのコメントは1つの投稿に属する
    public function post()
    {
        return $this->belongsTo(Post::class);
    }

    // 1つのコメントは1人のユーザーに属する
    public function author()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}

コントローラーでの使用

// app/Http/Controllers/PostController.php
<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    // 投稿とそのコメントを表示
    public function show($id)
    {
        // Eager Loadingで N+1 問題を回避
        $post = Post::with(['comments.author', 'author'])->findOrFail($id);
        
        return view('posts.show', compact('post'));
    }

    // 新しいコメントを追加
    public function addComment(Request $request, $postId)
    {
        $post = Post::findOrFail($postId);
        
        $post->comments()->create([
            'user_id' => auth()->id(),
            'content' => $request->content
        ]);

        return redirect()->back()->with('success', 'コメントが投稿されました');
    }

    // コメント数でフィルタリング
    public function popular()
    {
        $popularPosts = Post::withCount('comments')
            ->having('comments_count', '>', 10)
            ->orderBy('comments_count', 'desc')
            ->get();

        return view('posts.popular', compact('popularPosts'));
    }
}

ビューでの表示

{{-- resources/views/posts/show.blade.php --}}
<div class="post">
    <h1>{{ $post->title }}</h1>
    <p class="author">投稿者: {{ $post->author->name }}</p>
    <div class="content">{{ $post->content }}</div>

    <h2>コメント ({{ $post->comments->count() }}件)</h2>
    @foreach($post->comments as $comment)
        <div class="comment">
            <strong>{{ $comment->author->name }}</strong>
            <p>{{ $comment->content }}</p>
            <small>{{ $comment->created_at->diffForHumans() }}</small>
        </div>
    @endforeach
</div>

この機能の利点

  1. コードの簡潔性: 複雑なJOINクエリを書く必要がなく、直感的な構文でリレーションデータを取得できます
  2. Eager Loading: with()メソッドでN+1問題を簡単に回避できます
  3. 保守性: データベース構造の変更があっても、モデルの定義を変更するだけで対応できます

このように、Laravelのリレーションシップ機能は実際のアプリケーション開発で非常に強力かつ実用的です。

Laravel Tips botの投稿は基本的にOpenAI APIの出力です。現在はLaravel関連リリースノートの日本語訳が主。