简体   繁体   English

如何添加评论分页?

[英]How can I add pagination for comments?

I want to add paginations for my comments from posts.show .I've tried ->pagination(5) on my store function, but didn't work.Can you help me ? 我想为posts.show评论添加分页。我posts.show尝试在store功能上尝试->pagination(5) ,但没有用。您能帮我吗?

My CommentsController 我的CommentController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

use App\Comment;
use App\Post;
use Auth;
use Session;
use DB;

class CommentsController extends Controller
{

    public function __construct()
{
    $this->middleware('auth', ['except' => 'store']);
}


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, $post_id)
    {
        $this->validate($request, array(

            'comment' => 'required|min:5|max:2000',
        ));


        $post = Post::find($post_id);

        $comment = new Comment();
        $comment->username = Auth::user()->username;
        $comment->email = Auth::user()->email;
        $comment->user_id = Auth::user()->id;
        $comment->comment = $request->comment;
        $comment->image = Auth::user()->profile->image;
        $comment->approved = true;
        $comment->post()->associate($post);

        $comment->save();

        Session::flash('message', "Message posted successfully!");



        return Redirect::back();

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $comment = Comment::find($id);
        return view ('comments.edit')->withComment($comment);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $comment = Comment::find($id);

        $this->validate($request, array('comment' => 'required'));

        $comment->comment = $request->comment;
        $comment->save();

        Session::flash('success', 'Comment updated');


        return redirect('/post/' . $comment->post->id);
    }
    public function delete($id){

        DB::table('comments')->where('id',$id)->delete();

        Session::flash('remove', "Post was successfully removed!");




       return Redirect::back();
    }

    public function deleteMyComment($id){

        DB::table('comments')->where('id',$id)->delete();

       return Redirect::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

PostsController PostsController

<?php

namespace App\Http\Controllers;

use App\Post;
use App\Tag;
use Session;

use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\DB;

class PostsController extends Controller
{

    public function __construct(){
        $this->middleware('auth');
    }

    public function index(){
        $users = auth()->user()->following()->pluck('profiles.user_id');
        $posts = Post::whereIn('user_id', $users)->with('user')->latest()->paginate(10);

        return view('posts.index2', compact('posts'));
    }


 public function delete($id){

    if(!DB::table('posts')->where('id',$id)->where('user_id',auth()->user()->id)->delete()){
        Session::flash('remove', "You do not have permission to delete the post!");
    }

   return redirect('/profile/' . auth()->user()->id);
}


    public function create(){

        $tags = Tag::all();

        return view('posts.create')->withTags($tags);
    }
    public function store(Request $request )
    {



        $data = request()->validate([

            'caption' => 'required|max:255',
            'image' => 'required|image',
        ]);
    $imagePath = request('image')->store('uploads', 'public');

        $image = Image::make(public_path("storage/{$imagePath}"))->fit(1600, 1100);
        $image->save();



        $post = new Post;

        $post = auth()->user()->posts()->create([
        'caption' => $data['caption'],
        'image' => $imagePath,
    ]);

        $post->save();

        $post->tags()->sync($request->tags, false);

     return redirect('/profile/' . auth()->user()->id);
    }

    public function show(\App\Post $post){

        return view('posts.show', compact('post'));
    }

    public function editPost($id){

   $post = Post::find($id);

   $tags = Tag::all();
   $tags2 = array();
   foreach($tags as $tag){
        $tags2[$tag->id] = $tag->name;
   }


   return view ('posts.editPost')->withPost($post)->withTags($tags2);
}
    public function update(Request $request, $id)
    {
        $post = Post::find($id);

        $this->validate($request, array('caption' => 'required'));

        $post->caption = $request->caption;
        $post->save();

        $post->tags()->sync($request->tags, true);

        Session::flash('success', 'Post updated');

         return redirect('/post/' . $post->id);
    }

    public function destroy($id)
    {
        $post = Post::find($id);
        $post->tags()->detach();

        $post->delete();

        Session::flash('success', 'The post was successfully removed!');
        return redirect()->route('posts.index');
    }
}

My view 我的观点

<div class="col-md-12">
    @foreach($post->comments as $comment)
        <div class="media g-mb-30">
            <img class="d-flex g-width-50 g-height-50 rounded-circle g-mt-3 g-mr-20"
                 src="/storage/{{ $comment->image }}" alt="Image Description">
            <div class="media-body g-brd-around g-brd-gray-light-v4 g-pa-30" style="margin-right: -35px">
                <div class="g-mb-15">
                    <h5 class="d-flex justify-content-between align-items-center h5 g-color-gray-dark-v1 mb-0">
                        <span class="d-block g-mr-10">{{ $comment->username }}
                            <span class="g-color-black-opacity-0_7 g-pos-rel g-top-2 mx-2">&#183;</span>
                            <span class="g-color-gray-dark-v4 g-font-size-12">{{ $comment->created_at }}</span>
                        </span>
                        <a class="u-tags-v1 g-font-size-12 g-brd-around g-brd-gray-light-v4 g-bg-primary--hover g-brd-primary--hover g-color-black-opacity-0_8 g-color-white--hover rounded g-py-6 g-px-15"
                           href="/profile/{{ $comment->user_id }}">Author</a>
                    </h5>
                </div>

                <p>{{$comment->comment}}</p>

                <ul class="list-inline d-sm-flex my-0">
                    @can('update', $post->user->profile)
                        <li class="list-inline-item ml-auto">
                            <a class="u-link-v5 g-color-gray-dark-v4 g-color-primary--hover"
                               href="{{ route('comments.edit', $comment->id) }}">
                                <i class="icon-note g-pos-rel g-top-1 g-mr-3"></i>
                                Edit comment
                            </a>
                        </li>
                        <li class="list-inline-item ml-auto">
                            <a class="u-link-v5 g-color-gray-dark-v4 g-color-primary--hover"
                               href="/deleteComment/{{$comment->id}}">
                                <i class="icon-note g-pos-rel g-top-1 g-mr-3"></i>
                                Delete comment
                            </a>
                        </li>
                    @endcan
                </ul>
            </div>
        </div>
    @endforeach
</div>

try this 尝试这个

public function show($id){ //<= pass id here
   $post = Post::find($id);
   $comments = Comment::where('post_id', $post->id)->latest()->paginate(20);
   return view('posts.show', compact('post', 'comments'));
}

in view 鉴于

@foreach($comments as $comment)
  {{ $comment->message }}
@endforeach


{!! $comments->render() !!}

使您的foreach像这样:

 @foreach($post->comments()->paginate(5) as $comment)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM