Controllerでは、viewの指定なしにredirect()->back();と書ける。
PostCommentsController.php
public function index(){ $comments = Comment::all(); return view('admin.comments.index', compact('comments')); }
admin/comments/index.blade.php
@if($comments) <h1>Comments</h1> <table class="table table-striped"> <thead> <tr> <th>Id</th> <th>Author</th> <th>Email</th> <th>Body</th> <th>Post</th> </tr> </thead> <tbody> @foreach($comments as $comment) <tr> <td>{{$comment->id}}</td> <td>{{$comment->author}}</td> <td>{{$comment->email}}</td> <td>{{$comment->body}}</td> <td><a href="{{route('home.post',$comment->post_id)}}">View Post</td> <td> @if($comment->is_active == 1) {!! Form::open(['method'=>'PATCH', 'action'=>['PostCommentsController@update', $comment->id]]) !!} {{ csrf_field()}} <input type="hidden" name="is_active" value="0"> <div class="form-group"> {!! Form::submit('Un-approve', ['class'=>'btn btn-info']) !!} </div> {!! Form::close() !!} @else {!! Form::open(['method'=>'PATCH', 'action'=>['PostCommentsController@update', $comment->id]]) !!} {{ csrf_field()}} <input type="hidden" name="is_active" value="1"> <div class="form-group"> {!! Form::submit('Approve', ['class'=>'btn btn-info']) !!} </div> {!! Form::close() !!} @endif </td> <td> {!! Form::open(['method'=>'DELETE', 'action'=>['PostCommentsController@destroy', $comment->id]]) !!} {{ csrf_field()}} <div class="form-group"> {!! Form::submit('Delete', ['class'=>'btn btn-danger']) !!} </div> {!! Form::close() !!} </td> </tr> @endforeach </tbody> </table> @else <h1 class="text-center">No Comments</h1> @endif
PostCommentController.php
public function update(Request $request, $id){ Comment::findOrFail($id)->update($request->all()); return redirect('/admin/comments'); } public function destroy($id){ Comment::findOrFail($id)->delete(); return redirect()->back(); }
Post/index.blade.php
<td><a href="{{route('home.post', $post->id)}}">View Post</td> <td><a href="{{route('admin.comments.show', $post->id)}}">View Comments</td>
Post.php
public function comments(){ return $this->hasMany('App\Comment'); }
Route
Route::group(['middleware'=>'auth'], function(){ Route::post('comment/reply', 'CommentRepliesController@createReply'); });
post.blade.php
@if($comments) @foreach($comments as $comment) <!-- Comment --> <div class="media"> <a class="pull-left" href="#"> <img class="media-object" src="http://placehold.it/64x64" alt=""> </a> <div class="media-body"> <h4 class="media-heading">{{$comment->author}} <small>{{$comment->created_at->diffForhumans()}}</small> </h4> {{$comment->body}} @if($comment->replies) @foreach($comment->replies as $reply) <div class="nested-comment media"> <a class="pull-left" href="#"> <img class="media-object" src="http://placehold.it/64x64" alt=""> </a> <div class="media-body"> <h4 class="media-heading">{{$reply->author}} <small>{{$reply->created_at->diffForhumans()}}</small> </h4> {{$reply->body}} </div> {!! Form::open(['method'=>'POST', 'action'=>'CommentRepliesController@store']) !!} {{ csrf_field()}} <input type="hidden" name="comment_id" value="{{$comment->id}}"> <div class="form-group"> {!! Form::label('body', 'Body') !!} {!! Form::textarea('body', null, ['class'=>'form-control', 'rows'=>1]) !!} </div> <div class="form-group"> {!! Form::submit('Submit', ['class'=>'btn btn-primary']) !!} </div> {!! Form::close() !!} </div> @endforeach @endif
post.blade.php
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <script> $(".comment-reply-container .toggle-reply").click(function(){ $(this).next().slideToggle("slow"); }); </script>
replies/show.blade.php
@section('content') @if($replies) <h1>replies</h1> <table class="table table-striped"> <thead> <tr> <th>Id</th> <th>Author</th> <th>Email</th> <th>Body</th> <th>Post</th> </tr> </thead> <tbody> @foreach($replies as $reply) <tr> <td>{{$reply->id}}</td> <td>{{$reply->author}}</td> <td>{{$reply->email}}</td> <td>{{$reply->body}}</td> <td><a href="{{route('home.post',$reply->comment->post->id)}}">View Post</td> <td> @if($reply->is_active == 1) {!! Form::open(['method'=>'PATCH', 'action'=>['CommentRepliesController@update', $reply->id]]) !!} {{ csrf_field()}} <input type="hidden" name="is_active" value="0"> <div class="form-group"> {!! Form::submit('Un-approve', ['class'=>'btn btn-success']) !!} </div> {!! Form::close() !!} @else {!! Form::open(['method'=>'PATCH', 'action'=>['CommentRepliesController@update', $reply->id]]) !!} {{ csrf_field()}} <input type="hidden" name="is_active" value="1"> <div class="form-group"> {!! Form::submit('Approve', ['class'=>'btn btn-info']) !!} </div> {!! Form::close() !!} @endif </td> <td> {!! Form::open(['method'=>'DELETE', 'action'=>['CommentRepliesController@destroy', $reply->id]]) !!} {{ csrf_field()}} <div class="form-group"> {!! Form::submit('Delete', ['class'=>'btn btn-danger']) !!} </div> {!! Form::close() !!} </td> </tr> @endforeach </tbody> </table> @else <h1 class="text-center">No replies</h1> @endif @stop
comments/index.blade.php
<td><a href="{{route('admin.comments.replies.show', $comment->id)}}">View Replies</a></td>
CommentRepliesController.php
use App\Comment; public function show($id) { // $comment = Comment::findOrFail($id); $replies = $comment->replies; return view('admin.comments.replies.show', compact('replies')); } public function update(Request $request, $id) { // CommentReply::findOrFail($id)->update($request->all()); return redirect()->back(); } public function destroy($id) { // CommentReply::findOrFail($id)->delete(); return redirect()->back(); }