モデル部分
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Micropost extends Model { protected $fillable = [ 'user_id', 'image_path', 'search_tag', 'map_lat', 'map_long' ]; public function user() { return $this->belongsTo(User::class); } public function comments() { return $this->hasMany(Comment::class); } }
コントローラー部分
php
1<?php 2 3namespace App\Http\Controllers; 4use Illuminate\Http\Request; 5use App\Http\Requests; 6use App\Http\Controllers\Controller; 7use App\User; 8use App\Micropost; 9use App\Comment; 10 11class CommentsController extends Controller 12{ 13 14 public function index($micropost_id) 15 { 16 $comments = new Comment; 17 $micropost = Micropost::find($micropost_id); 18 $comments = $micopost->comments()->orderBy('created_at', 'desc')->paginate(10); 19 return view('microposts.show', ['comments' => $comments]); 20 } 21 22 public function store(Request $request, $micropost_id) 23 { 24 $micropost = Micropost::find($micropost_id); 25 $this->validate($request, ['comment' => 'required|max:191', ]); 26 $micropost->comments()->create([ 27 'user_id' => \Auth::User()->id, 28 'micropost_id' => $micropost_id, 29 'comment' => $request->comment, 30 ]); 31 return redirect()->back(); 32 } 33}
viewコメント一覧部分 microposts.show
<ul class="media-list"> @foreach($comments as $comment)ここでUndefined variable: comments とエラーが出る <li class="media"> <div class="media-right"> <div class="media-body"> <div> <p>{{ $comment->comment }}</p> </div> </div> </div> </li> @endforeach </ul>
viewコメント入力部分 microposts.show
<p>コメント</p> <div class="row"> <div class="col-xs-12"> {!! Form::open(['action' => ['CommentsController@store', $micropost->id]]) !!} <div class="form-group"> {!! Form::textarea('comment', old('comment'), ['class' => 'form-control', 'rows' => '2']) !!} </div> {!! Form::submit('New comment', ['class' => 'btn btn-warning btn-block']) !!} </div> </div>
ルーティングは以下の通り
Route::group(['middleware' => 'auth'], function () { Route::resource('users', 'UsersController', ['only' => ['index', 'show', 'edit', 'update', 'destroy']]); Route::group(['prefix' => 'users/{id}'], function () { Route::post('follow', 'UserFollowController@store')->name('user.follow'); Route::delete('unfollow', 'UserFollowController@destroy')->name('user.unfollow'); Route::get('followings', 'UsersController@followings')->name('users.followings'); Route::get('followers', 'UsersController@followers')->name('users.followers'); Route::post('favorite', 'FavoritesController@store')->name('user.favorite'); Route::delete('unfavorite', 'FavoritesController@destroy')->name('user.unfavorite'); Route::get('favoritings', 'UsersController@favoritings')->name('users.favoritings'); }); Route::get('microposts/search', 'MicropostsController@search'); Route::get('microposts/maps', 'MicropostsController@maps')->name('microposts.maps'); Route::resource('microposts', 'MicropostsController', ['only' => ['create', 'store', 'show', 'edit', 'update', 'destroy']]); Route::group(['prefix' => 'microposts/{id}'], function () { Route::resource('comment', 'CommentsController', ['only' => ['index', 'store', 'destroy']]); });
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/05 07:20