本記事について
本記事は私が学習していく中でわからない単語や概要をなるべくわかりやすい様にまとめたものです。
もし誤りなどありましたらコメントにてお知らせいただけるとありがたいです。
概要
chatAPIを使用してアプリを作る。
laravel10,php8.2
前回の記事
今回やること
・コメント機能を作る
コメント機能を作る
マイグレーションの作成
コメントテーブルを作成するためのマイグレーションを作成します。
php artisan make:migration create_comments_table
database/migrations/create_comments_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCommentsTable extends Migration
{
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('question_id')->constrained();
$table->text('comment');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('comments');
}
}
モデルの作成
コメントモデルを作成します。
php artisan make:model Comment
app/Models/Comment.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use HasFactory;
protected $fillable = ['question_id', 'comment'];
public function question()
{
return $this->belongsTo(Question::class);
}
}
モデル間のリレーションシップ
質問モデルにコメントとのリレーションシップを追加します。
リレーションシップとは
関係性、結びつき
今回は質問とコメントついて関係性
app/Models/Question.php
public function comments()
{
return $this->hasMany(Comment::class);
}
を追記
コントローラーの更新
app/Http/Controllers/ChatGPTController.php
use App\Models\Comment;
//こちらに修正
public function index()
{
$questions = Question::with(['answers', 'comments'])->get();
return view('questions.index', compact('questions'));
}
// コメントの追加メソッド
public function addComment(Request $request, Question $question)
{
$request->validate([
'comment' => 'required|string|max:255',
]);
$question->comments()->create([
'comment' => $request->input('comment'),
]);
return redirect()->back();
}
を追記、修正
$questions = Question::with(['answers', 'comments'])->get();
このコードは、questions テーブルから全ての質問を取得し、その質問に関連する全ての回答 (answers) とコメント (comments) も一度に取得します。
ルート追加
コメントを追加するためのルートを設定します。
Route::post('/questions/{question}/comment', [ChatGPTController::class, 'addComment']);
ビューファイルの更新
質問と回答の一覧表示にコメントフォームを追加します。
resources/views/questions/index.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>質問一覧</title>
</head>
<body>
<h1>質問一覧</h1>
@if($questions && count($questions) > 0)
@foreach($questions as $question)
<div>
<h3>{{ $question->question }}</h3>
@if($question->answers && count($question->answers) > 0)
@foreach($question->answers as $answer)
<p>{{ $answer->answer }}</p>
<form action="/like/{{ $answer->id }}" method="post">
@csrf
<button type="submit">いいね ({{ $answer->likes }})</button>
</form>
@endforeach
@endif
<h4>コメント:</h4>
@if($question->comments && count($question->comments) > 0)
@foreach($question->comments as $comment)
<p>{{ $comment->comment }}</p>
@endforeach
@endif
<form action="/questions/{{ $question->id }}/comment" method="post">
@csrf
<textarea name="comment" rows="3" required></textarea>
<button type="submit">コメント追加</button>
</form>
</div>
@endforeach
@else
<p>質問がありません。</p>
@endif
</body>
</html>