0
votes

I am working on a project in laravel 5.3. every thing is gong well but I come to face with a pagination problem in a view. I think, I am missing some thing in code and unable to detect what I have missed.

I have posts table and a categories table and another table which stores the the relations of posts and categories its name is post_n_category. following image show the structure of post_n_category table.

enter image description here

I have coded the method to fetch data from database whithin the model.

public static function pids($id)
{
    $posts = self::select('pid')->where('sid', $id)->orderby('id', 'desc')->paginate(5);

    if($posts != null)
    {
        return $posts;
    }
    else
    {
        return "";
    }

}

and follwing is the controller method which return view.

public function category()
{
    return view('category');
}

and finally following is the view code.

@foreach(\App\PnC::pids(9) as $pid)

    {{ $pid->pid }}<br />

@endforeach
<hr />
{{ \App\PnC::pids(9)->links() }}

This code is working correctly, pids display correctly and buttons of paginations are visible as thet should be. but the problem id that when i click on page 2. the url changes correctly but content remain same as first page and the active link of pagination links remain as 1 according the content. so in short the problem is thet on clicking on page number url in address bar chanches correctly but content and active link of pagination links remain same as first page.. so any help please..

1

1 Answers

0
votes

Can I suggest a different approach. In your controller use

public function category()
{
        $posts = Posts::select('pid')->where('sid', $id)->orderby('id', 'desc')->paginate(5);

    return view('category', compact ('posts'));
}

In your view

@foreach($posts as $post)

    {{ $post->pid }}<br />

@endforeach

{{ $posts->links() }}

While using pagination it's better to call function in controller itself since laravel provide pagination by default. You can find reference here