Dracool's avatar

Pagination + query results (not working as intended)

I have 15 posts in my blogposts database and I want laravel to show 5 of them on page (would take 3 pages) in total.

But both the query and pagination don't work.

For some reason, the query returns only 4 items and the pagination doesn't work at all (even though pagination cuts the items selected when I specify how many)

$data = DB::table('blogposts')
        ->select('*')
        ->join('blogcategories', 'blogposts.postid', '=', 'blogcategories.id')
        ->paginate(5);
@foreach ($all['all'] as $post)
        <table class="table">
            <tr>
                <td>
                <tr><th><h2><kbd><font color='lime'>{{ $post->postname }}</font></kbd></h2>
                <kbd><font color='cyan'>{{ $post->postdate }}</font></kbd>
                <kbd><font color='red'>{{ $post->catname }}</font></kbd></th></tr>
                <tr><td><blockquote><font color='gray'>{{ mb_substr($post->postdesc, 0,120,'UTF-8') }}</font>
                <a href="{{ URL::to('/posts/').'/'.$post->postid }}">read more</a></blockquote>
                </td>
            </tr>
        </table>
    @endforeach
                {!! $post->render() !!}
@stop

What am I doing wrong? Do I need to include something for pagination to work (though following the documentation, I see nothing like that)

0 likes
16 replies
bestmomo's avatar

I think it's :

{!! $all['all']->links() !!}

But I cant see your controller...

Dracool's avatar

@bestmomo Thanks for the reply. I tried what you wrote, didn't work.

I have nothing included in my controller, its just a basic plain controller. Here is the function for the blogposts though:

public function grabPosts()
    {
        $model = [];
        $model['all'] = Blogposts::getAllPosts();
        /*dd($model);*/
        return View::make('pages.posts', array('all' => $model));       
    }
bestmomo's avatar

Why do you set a key ? Simply do that :

public function grabPosts()
    {
        $all = Blogposts::getAllPosts();
        return view('pages.posts', compact('all');       
    }

And in your view :

@foreach ($all as $post)
...

And then :

{!! $all->links() !!}

should work...

Dracool's avatar

@bestmomo Tried what you said, but now it throws a:

ErrorException (E_ERROR) HELP Undefined variable: all (View: C:\xampp\htdocs\laravel\app\views\pages\posts.blade.php)

Controller

public function grabPosts()
    {
        /*$model = [];
        $model['all'] = Blogposts::getAllPosts();
        dd($model);*/
        $all = Blogposts::getAllPosts();
        return View::make('pages.posts', compact($all));        
    }

View

@extends('app')

@section('content')

<h1>All Posts</h1>

@foreach ($all as $post)
        <table class="table">
            <tr>
                <td>
                <tr><th><h2><kbd><font color='lime'>{{ $post->postname }}</font></kbd></h2>
                <kbd><font color='cyan'>{{ $post->postdate }}</font></kbd>
                <kbd><font color='red'>{{ $post->catname }}</font></kbd></th></tr>
                <tr><td><blockquote><font color='gray'>{{ mb_substr($post->postdesc, 0,120,'UTF-8') }}</font>
                <a href="{{ URL::to('/posts/').'/'.$post->postid }}">read more</a></blockquote>
                </td>
            </tr>
        </table>
    @endforeach
                {!! $post->links() !!}
@stop
d3xt3r's avatar

1>

 return view('pages.posts', compact('all');       // notice 'all' and not $all;

2> Have you checked if query indeed has 15 elements

$data = DB::table('blogposts')
        ->select('*')
        ->join('blogcategories', 'blogposts.postid', '=', 'blogcategories.id')->get();
     
dd($data);
Dracool's avatar

@premsaurav Oh, didn't see that. Thanks!

Ok, so it works like @bestmomo said, but pagination and query still don't work.

Yes I'm sure. I can send you a screenshot of the database if you want, since its a dummy base I use for learning stuff. I also tested with the dd thing - it returned a huge text. But it only picked items from the database to the 4th item. So I guess it only picked the first 4 items and then it stops.

d3xt3r's avatar

Yes please share the screenshot, as far as i can tell, this has nothing to do with paginate then, but may be the join you are performing .

Dracool's avatar
d3xt3r's avatar
d3xt3r
Best Answer
Level 29

yes , as expected its the join that's wrong

$data = DB::table('blogposts')
        ->select('*')
        ->join('blogcategories', 'blogposts.postcatid', '=', 'blogcategories.id')
        ->paginate(5);
Dracool's avatar

@premsaurav Now I just feel silly for wasting people's time. I'm still grateful for the help though.

Query works, but pagination still doesn't work.

d3xt3r's avatar

@Dracool What do you mean by pagination still doesn't work ? What is that you are expecting and what is that you are getting ?

Dracool's avatar

@premsaurav The query works now, but pagination doesn't. I asked when I opened the thread should I include something somewhere that deals with pagination because I have nothing included in any of the mvc components.

Pagination doesn't put links at the bottom, it just writes the raw code. I tried with {!! $all->render() !!}, {!! $all->links() !!}, {!! $post->render() !!} and {!! $post->links() !!} but none of those work, laravel just puts that code at the end of the page.

d3xt3r's avatar

@Dracool Can't debug further unless I see your complete M V and C for this problem.

Dracool's avatar

@premsaurav You want only the mvc or the entire laravel folder? I don't know if I can even attach something here.

d3xt3r's avatar

If its shared on github, even better. However, just need the files(code) related to this problem. You can paste them here as well.

ben_s247's avatar

any chance you have blogposts as a model

then you can do something like this assuming you have one category per blog post otherwise the relationship would be a little different part of App\Blogpost.php

class BlogPost extends Model
{
    protected $table = 'blogposts';

 protected $fillable = ['all', 'columns', 'filled_in', 'by_form'];

   public function category()
   {
        return $this->belongsTo(App\BlogCategory::class);
   }

}

App\BlogCategory.php

    public function posts()
    {
        return $this->hasMany(App\Blogpost::class');
    }

then in the controller it would be

$all = App\BlogPost::with(['category'])->paginate(5);

bit of a refactor but if you can avoid writing joins like that all the better so in the view just reference pages/posts.blade.php

@foreach($all as $post)
    <table class="table">
            <tr>
                <td>
                <tr><th><h2><kbd><font color='lime'>{{ $post->postname }}</font></kbd></h2>
                <kbd><font color='cyan'>{{ $post->postdate }}</font></kbd>
                <kbd><font color='red'>{{ $post->category() }}</font></kbd></th></tr>
                <tr><td><blockquote><font color='gray'>{{ mb_substr($post->postdesc, 0,120,'UTF-8') }}</font>
                <a href="{{ URL::to('/posts/').'/'.$post->postid }}">read more</a></blockquote>
                </td>
            </tr>
        </table>
@endforeach 
{{!! $all->render() !!}

Please or to participate in this conversation.