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

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

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

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

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

MAMP

Mac 上で WordPress などの動的ページのサイトが作れるように環境を構築するフリーソフト

Q&A

解決済

1回答

382閲覧

掲示板サイトのコメントが表示されない

ishidakouiki

総合スコア73

PHP

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

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

MAMP

Mac 上で WordPress などの動的ページのサイトが作れるように環境を構築するフリーソフト

0グッド

0クリップ

投稿2022/05/03 05:29

掲示板サイトの実装を行なっていて、
コメント機能の実装でindex.blade.phpに投稿したコメントが表示されない状態で、
indexblade.phpに
Invalid argument supplied for foreach() (View: /Applications/MAMP/htdocs/bike/resources/views/posts/index.blade.php)
とエラーが表示されます。
foreach()に無効な引数が指定されました.
forreach()中の無効な引数→$post->comments の値が入っていないという意味なのでしょうか?
DBを確認すると、投稿したコメントは反映されている状態です。
イメージ説明
またpost(投稿)機能のcrudは正常に機能している状態です。

index.blade.php

1〜省略〜 2@foreach ($posts as $post) 3<div class="col-md-8 col-md-2 mx-auto"> 4 <div class="card-wrap"> 5 <div class="card mt-3"> 6 <div class="card-header align-items-center d-flex"> 7 <a class="no-text-decoration" href=""> 8 <i class="fas fa-user-circle fa-2x mr-1"></i> 9 </a> 10 <a class="black-color" title="" href=""> 11 <strong> 12 出品者 13 </strong> 14 </a> 15 </div> 16 〜省略〜 17 <h3 class="h5 title"> 18 {{ $post->name }} 19 </h3> 20 <div class="mb-5"> 21 {{ $post->year }} 22 </div> 23 <div class="mb-5"> 24 {{ $post->price }} 25 </div> 26 <div class="mb-5"> 27 {{ $post->explanation }} 28 </div> 29 <section> 30 {{-- <span class="help-block"> 31 @include('commons.error_messages') 32 </span> --}} 33 @foreach($post->comments as $comment) 34 <div class="container mt-4 text-left"> 35 <div class="border-top p-1"> 36 <span> 37 @if (Auth::id() == $comment->user->id) 38 <div class="post_edit"> 39 <form class="edit_button" method="get" action="{{ route('comments.edit', $comment->id ) }}"> 40 @csrf 41 <button class="btn btn-size btn-primary"><i class="far fa-edit"></i>編集</button> 42 </form> 43 <form class="edit_button" method="post" action="{{ route('comments.destroy', $comment->id )}}" accept-charset="UTF-8"> 44 @csrf 45 @method('DELETE') 46 <button type="submit" class="btn btn-size btn-danger" rel="nofollow" ><i class="far fa-trash-alt"></i>削除</button> 47 </form> 48 </div> 49 @endif 50 <strong> 51 <a class="no-text-decoration black-color" href="{{ route('users.show', $comment->user->id ) }}"> 52 {{$comment->user->name}} 53 </a> 54 </strong> 55 </span> 56 <div class="comments mt-1"> 57 <span> 58 {{$comment->comment}} 59 </span> 60 </div> 61 </div> 62 </div> 63 @endforeach 64 <div id="comment-post-1"> 65 <div class="m-4"> 66 <form class="w-100" action="{{ route('comments.store') }}" method="post"> 67 @csrf 68 <input name="utf8" type="hidden" value=""/> 69 <input value="" type="hidden" name="user_id" /> 70 <input value="{{ $post->id }}" type="hidden" name="post_id" /> 71 <input name="comments[{{ $post->id }}]" value="{{ old("comments.$post->id") }}" class="form-control comment-input border border-light mx-auto" placeholder="コメントを入力する"> 72 </input> 73 <div class="text-right"> 74 <input type="submit" value="&#xf075;コメント送信" class="far fa-comment btn btn-default btn-sm"> 75 </input> 76 </div> 77 </form> 78 </div> 79 </div> 80 </section> 81 </div> 82 </div> 83 </section> 84@endforeach 85 86@endsection

web.php

1~省略~ 2Route::get('bike/new','PostsController@index')->name('post.index');//バイク販売ページ表示// 3Route::get('post/new','PostsController@create')->name('post.create');//バイク投稿ページ表示// 4Route::post('/','PostsController@store')->name('post.store');//バイク投稿機能// 5Route::get('post/{id}/edit','PostsController@edit')->name('post.edit');//バイク投稿情報編集機能// 6Route::post('post/{id}','PostsController@update')->name('post.update');//バイク投稿更新機能// 7Route::delete('post/{id}','PostsController@delete')->name('post.destroy');//バイク投稿削除機能// 8 9Route::post('comments', 'CommentsController@store')->name('comments.store');//コメント投稿機能// 10Route::get('comments/{id}/edit', 'CommentsController@edit')->name('comments.edit'); 11Route::put('comments/{id}', 'CommentsController@update')->name('comments.update'); 12Route::delete('comments/{id}', 'CommentsController@destroy')->name('comments.destroy');

PostController.php(投稿)

1<?php 2class PostsController extends Controller 3{ 4 public function index() 5 { 6 $posts = Post::orderBy('created_at', 'desc')->get(); 7 return view('posts.index', ['posts' => $posts]); 8 } 9 10 public function create() 11 { 12 return view('posts.create'); 13 } 14 15 public function store(PostRequest $request) 16 { 17 $post = new Post(); 18 $post->user_id = Auth::id(); 19 $post->name = $request->name; 20 $post->year = $request->year; 21 $post->price = $request->price; 22 $post->explanation = $request->explanation; 23 $post->save(); 24 25 return redirect()->route('index'); 26 } 27 public function edit($id) 28 { 29 //indexから渡されたidのpostテーブル情報を全て取得 30 $post = post::find($id); 31 //取得したユーザーidと認証しているidが違う場合トップページに戻りエラー表示を出す 32 if($post->user_id !== \Auth::id()){ 33 return redirect()->route('index') 34 ->with('error', '許可されていない操作です'); 35 } 36 //editページに画面遷移しpost情報を渡す 37 return view('posts.edit', compact('post')); 38 } 39 public function update(PostRequest $request, $id){ 40 //postテーブルから自分のidのテーブル情報を取得 41 $post = Post::find($id); 42 //認証しているuser_idと投稿したidが等しかったら変更内容をDBに保存 43 if( Auth::id() == $post->user_id) { 44 $post->name = $request->name; 45 $post->year = $request->year; 46 $post->price = $request->price; 47 $post->explanation = $request->explanation; 48 $post->save(); 49 50 return redirect()->route('index'); 51 } 52 53 return redirect()->route('index')->with('error', '許可されていない操作です'); 54 } 55 56 public function delete($id) 57 { 58 $post = post::find($id); 59 60 if(Auth::id() == $post->user_id) 61 { 62 $post -> delete(); 63 64 return redirect()->route('index'); 65 } 66 return redirect()->route('index')->with('error','許可されていない操作です'); 67 } 68 69}

CommentsController.php

1<?php 2class CommentsController extends Controller 3{ 4 public function store(CommentPostRequest $request) 5 { 6 $comment = new Comment(); 7 $comment->user_id = Auth::id();//認証しているuser_idを$commnetのuser_idに入れる 8 $comment->post_id = $request->post_id;// 9 $comment->comment = $request->comments[$request->post_id]; 10 $comment->save(); 11 12 return redirect()->route('index'); 13 } 14 15 public function edit($id) { 16 $comment = Comment::findOrFail($id); 17 return view('comments.edit', compact('comment')); 18 } 19 20 public function update(CommentEditRequest $request, $id) { 21 $comment = Comment::findOrFail($id); 22 23 if(Auth::id() == $comment->user->id){ 24 $comment->comment = $request->comments; 25 $comment->save(); 26 27 return redirect()->route('index'); 28 } 29 30 return redirect()->route('index') 31 ->with('error', '許可されていない操作です'); 32 } 33 34 public function destroy($id) { 35 36 $comment = Comment::findOrFail($id); 37 38 if(Auth::id() !== $comment->user->id){ 39 return redirect()->route('index') 40 ->with('error', '許可されていない操作です'); 41 } 42 43 $comment -> delete(); 44 return redirect()->route('index'); 45 46 } 47} 48

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

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

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

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

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

phper.k

2022/05/03 05:48 編集

Models/Post.php の中身がひつようですね リレーションの設定していないのでは?
ishidakouiki

2022/05/03 06:34

Post.phpの記述内容に間違いがありました! ご指摘いただきありがとうございました!
guest

回答1

0

自己解決

commentsと記述しないといけないのが、
commentと記述しておりました。

Post.php

1public function comments() 2 { 3 return $this->hasMany(Comment::class); 4 }

投稿2022/05/03 06:36

ishidakouiki

総合スコア73

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問