掲示板サイトの実装を行なっていて、
コメント機能の実装で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
〜省略〜 @foreach ($posts as $post) <div class="col-md-8 col-md-2 mx-auto"> <div class="card-wrap"> <div class="card mt-3"> <div class="card-header align-items-center d-flex"> <a class="no-text-decoration" href=""> <i class="fas fa-user-circle fa-2x mr-1"></i> </a> <a class="black-color" title="" href=""> <strong> 出品者 </strong> </a> </div> 〜省略〜 <h3 class="h5 title"> {{ $post->name }} </h3> <div class="mb-5"> {{ $post->year }} </div> <div class="mb-5"> {{ $post->price }} </div> <div class="mb-5"> {{ $post->explanation }} </div> <section> {{-- <span class="help-block"> @include('commons.error_messages') </span> --}} @foreach($post->comments as $comment) <div class="container mt-4 text-left"> <div class="border-top p-1"> <span> @if (Auth::id() == $comment->user->id) <div class="post_edit"> <form class="edit_button" method="get" action="{{ route('comments.edit', $comment->id ) }}"> @csrf <button class="btn btn-size btn-primary"><i class="far fa-edit"></i>編集</button> </form> <form class="edit_button" method="post" action="{{ route('comments.destroy', $comment->id )}}" accept-charset="UTF-8"> @csrf @method('DELETE') <button type="submit" class="btn btn-size btn-danger" rel="nofollow" ><i class="far fa-trash-alt"></i>削除</button> </form> </div> @endif <strong> <a class="no-text-decoration black-color" href="{{ route('users.show', $comment->user->id ) }}"> {{$comment->user->name}} </a> </strong> </span> <div class="comments mt-1"> <span> {{$comment->comment}} </span> </div> </div> </div> @endforeach <div id="comment-post-1"> <div class="m-4"> <form class="w-100" action="{{ route('comments.store') }}" method="post"> @csrf <input name="utf8" type="hidden" value=""/> <input value="" type="hidden" name="user_id" /> <input value="{{ $post->id }}" type="hidden" name="post_id" /> <input name="comments[{{ $post->id }}]" value="{{ old("comments.$post->id") }}" class="form-control comment-input border border-light mx-auto" placeholder="コメントを入力する"> </input> <div class="text-right"> <input type="submit" value="コメント送信" class="far fa-comment btn btn-default btn-sm"> </input> </div> </form> </div> </div> </section> </div> </div> </section> @endforeach @endsection
web.php
~省略~ Route::get('bike/new','PostsController@index')->name('post.index');//バイク販売ページ表示// Route::get('post/new','PostsController@create')->name('post.create');//バイク投稿ページ表示// Route::post('/','PostsController@store')->name('post.store');//バイク投稿機能// Route::get('post/{id}/edit','PostsController@edit')->name('post.edit');//バイク投稿情報編集機能// Route::post('post/{id}','PostsController@update')->name('post.update');//バイク投稿更新機能// Route::delete('post/{id}','PostsController@delete')->name('post.destroy');//バイク投稿削除機能// Route::post('comments', 'CommentsController@store')->name('comments.store');//コメント投稿機能// Route::get('comments/{id}/edit', 'CommentsController@edit')->name('comments.edit'); Route::put('comments/{id}', 'CommentsController@update')->name('comments.update'); Route::delete('comments/{id}', 'CommentsController@destroy')->name('comments.destroy');
PostController.php(投稿)
<?php class PostsController extends Controller { public function index() { $posts = Post::orderBy('created_at', 'desc')->get(); return view('posts.index', ['posts' => $posts]); } public function create() { return view('posts.create'); } public function store(PostRequest $request) { $post = new Post(); $post->user_id = Auth::id(); $post->name = $request->name; $post->year = $request->year; $post->price = $request->price; $post->explanation = $request->explanation; $post->save(); return redirect()->route('index'); } public function edit($id) { //indexから渡されたidのpostテーブル情報を全て取得 $post = post::find($id); //取得したユーザーidと認証しているidが違う場合トップページに戻りエラー表示を出す if($post->user_id !== \Auth::id()){ return redirect()->route('index') ->with('error', '許可されていない操作です'); } //editページに画面遷移しpost情報を渡す return view('posts.edit', compact('post')); } public function update(PostRequest $request, $id){ //postテーブルから自分のidのテーブル情報を取得 $post = Post::find($id); //認証しているuser_idと投稿したidが等しかったら変更内容をDBに保存 if( Auth::id() == $post->user_id) { $post->name = $request->name; $post->year = $request->year; $post->price = $request->price; $post->explanation = $request->explanation; $post->save(); return redirect()->route('index'); } return redirect()->route('index')->with('error', '許可されていない操作です'); } public function delete($id) { $post = post::find($id); if(Auth::id() == $post->user_id) { $post -> delete(); return redirect()->route('index'); } return redirect()->route('index')->with('error','許可されていない操作です'); } }
CommentsController.php
<?php class CommentsController extends Controller { public function store(CommentPostRequest $request) { $comment = new Comment(); $comment->user_id = Auth::id();//認証しているuser_idを$commnetのuser_idに入れる $comment->post_id = $request->post_id;// $comment->comment = $request->comments[$request->post_id]; $comment->save(); return redirect()->route('index'); } public function edit($id) { $comment = Comment::findOrFail($id); return view('comments.edit', compact('comment')); } public function update(CommentEditRequest $request, $id) { $comment = Comment::findOrFail($id); if(Auth::id() == $comment->user->id){ $comment->comment = $request->comments; $comment->save(); return redirect()->route('index'); } return redirect()->route('index') ->with('error', '許可されていない操作です'); } public function destroy($id) { $comment = Comment::findOrFail($id); if(Auth::id() !== $comment->user->id){ return redirect()->route('index') ->with('error', '許可されていない操作です'); } $comment -> delete(); return redirect()->route('index'); } }
まだ回答がついていません
会員登録して回答してみよう