質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

0回答

1862閲覧

[PHP・Laravel]変数が定義されていない。コメント機能実装中でのエラー。

退会済みユーザー

退会済みユーザー

総合スコア0

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2021/05/15 02:19

前提・実現したいこと

掲示板アプリを作成しております。
詳細ページにおいてコメントを投稿する機能を実装中なのですが、エラーで躓いて解決できない状況です。
ご教授いただけると幸いです。

発生している問題・エラーメッセージ

ErrorException Undefined variable: comments (View: /var/www/html/resources/views/articles/show.blade.php) $comments is undefined Make the variable optional in the blade template. Replace {{ $comments }} with {{ $comments ?? '' }}

イメージ説明

該当のソースコード

web.php

PHP

1// 略 2 3Route::resource('/comments', 'CommentController')->except(['index', 'show'])->middleware('auth');

Comment.php

PHP

1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6use Illuminate\Database\Eloquent\Relations\BelongsTo; 7 8class Comment extends Model 9{ 10 protected $fillable = [ 11 'body', 12 ]; 13 14 public function user(): BelongsTo 15 { 16 return $this->belongsTo('App\User'); 17 } 18 19 public function article(): BelongsTo 20 { 21 return $this->belongsTo('App\Article'); 22 } 23}

CommentController.php

PHP

1<?php 2 3namespace App\Http\Controllers; 4 5use App\Comment; 6use App\Http\Requests\CommentRequest; 7use Illuminate\Http\Request; 8 9class CommentController extends Controller 10{ 11 public function store(CommentRequest $request, Comment $comment) 12 { 13 $comment->fill($request->all()); 14 $comment->user_id = $request->user()->id; 15 $comment->article_id = $request->article()->id; 16 $comment->save(); 17 return redirect()->route('articles.show'); 18 } 19 20 // 略 21}

show.blade.php

PHP

1@extends('app') 2 3@section('title', '詳細ページ') 4 5@section('content') 6 @include('nav') 7 <div class="container"> 8 @include('articles.card') 9 </div> 10 <div class=h4>コメント</div> 11 <div> 12 @foreach($comments as $comment) 13 <div class="font-weight-bold">{{ $comment->user->name }}</div> 14 <div class="font-weight-lighter">{{ $comment->created_at->format('Y/m/d H:i') }}</div> 15 <div>{{ $comment->body }}</div> 16 @endforeach 17 </div> 18 19 <form method="POST" action="{{ route('comments.store') }}"> 20 @csrf 21 <label></label> 22 <div class="form-group"> 23 <textarea name="body" class="form-control" rows="16" placeholder="コメントを入力してください">{{ $comment->body ?? old('body') }}</textarea> 24 </div> 25 <button type="submit" class="btn blue-gradient btn-block">投稿する</button> 26 </form> 27@endsection

マイグレーションファイル

PHP

1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateCommentsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('comments', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->text('body'); 19 $table->bigInteger('user_id')->unsigned(); 20 $table->bigInteger('article_id')->unsigned(); 21 $table->timestamps(); 22 }); 23 } 24 25 /** 26 * Reverse the migrations. 27 * 28 * @return void 29 */ 30 public function down() 31 { 32 Schema::dropIfExists('comments'); 33 } 34} 35

試したこと

〇 この質問とエラー内容が類似していたので、参考にさせていただきました。
・ルーティングの重複はなし。
・CommentController.phpにuse App\Http\Controllers\Controller;を追記→特に変化なし。

〇 Laravel Document リクエストの取得の書き方を真似ながら、CommentController.phpのstoreアクションを下記のように修正しました。

public function store(CommentRequest $request) { $comment = new Comment; $comment->body = $request->input('body'); $comment->save(); return redirect()->route('articles.show'); }

特に変化はありませんでした。

補足情報(FW/ツールのバージョンなど)

PHP 7.4.1
Laravel 6.20.26
Docker 20.10.6
docker-compose 1.29.1
windowsを使用。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

phper.k

2021/05/15 06:40

CommentController の 全体を提示しないと問題点が含まれていない。
退会済みユーザー

退会済みユーザー

2021/05/15 07:00 編集

コメントありがとうございます。 storeアクション以外はまだ実装していなかったので、省略していました。 CommentController.phpを全て記載します。 <?php namespace App\Http\Controllers; use App\Comment; use App\Http\Requests\CommentRequest; use Illuminate\Http\Request; class CommentController extends Controller { public function store(CommentRequest $request, Comment $comment) { $comment->fill($request->all()); $comment->user_id = $request->user()->id; $comment->article_id = $request->article()->id; $comment->save(); return redirect()->route('articles.show'); } public function edit() { // } public function update() { // } public function destroy() { // } }
phper.k

2021/05/15 07:25 編集

'articles.show' これにリダイレクトしてるんだから、ArticleControllerのshow メソッドじゃないの? web.php に定義したルーティングで、articles.show にバインディングしているコントローラーの実装を確認してください
退会済みユーザー

退会済みユーザー

2021/05/15 08:29

ご指摘ありがとうございます。 しばらく考えてみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問