CaptainBurah's avatar

{{$posts->links()}} : Call to Undefined Method

I have tried to use a pagination but its says "Call to undefined Method"

0 likes
16 replies
Nakov's avatar

And how does your controller code looks like?

also try links() instead of Links().

1 like
CaptainBurah's avatar

Sorry my bad, i did use 'links()'

Here is my controller code....

    {
        $posts = Post::orderBy('title', 'desc')->paginate(10);
        return view('posts.index')->with('posts', $posts);
    }
Nakov's avatar

@captainburah how sure are you that the error comes from using $posts->links()?

Can you remove that from the page, and try to reload to ensure that the page loads without using that?

1 like
CaptainBurah's avatar

Without the code {{$posts->links()}} the page loads fine as called by the controller. When i insert the pagination, it prompts that error "Call to Method Undefined".

But i did change my Bootstrap 3 to Bootstrap 4 recently.

Nakov's avatar

Bootstrap has nothing to do with it.

I just doubt that on the page where you use $posts->links() you are not using a paginated results.

Try dumping the results of $posts in your controller and make sure that the object is an instance of a Paginator.

You can do that using dd($posts);. If it doesn't stops, that means that the posts are coming in from somewhere else. In case you use a View Composer and you share the posts from within the composer, than this values will be overwritten.

1 like
CaptainBurah's avatar

Paginated results? Im sorry, im learning laravel, began this week. Where do i place the dd($posts); in the controller?

Here is the code of my 'Views'...

@extends('layouts.app')

@section('content')
    <h1>POSTS</h1>
    @if(count($posts)>0)
            @foreach($posts as $posts)
                <div class="card card-body mt-3">
                    <h3><a href="/posts/{{$posts->id}}">{{$posts->title}}</h3>
                    <small>Written on {{$posts->created_at}}</small>
                </div>
            @endforeach
            {{$posts->links()}}
    @else
        <p>No Posts Found</p>
    @endif

@endsection

Here is the code of my 'Controller'...

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
    public function index()
    {
        $posts = Post::orderBy('title', 'desc')->paginate(10);
        return view('posts.index')->with('posts', $posts);
    }
Nakov's avatar

Are you copy pasting the full files or just partials? Because your controller does not look good if that's the full content.

you place it below the initialization in the controller:

$posts = Post::orderBy('title', 'desc')->paginate(10);

dd($posts);
1 like
CaptainBurah's avatar

No i just copy pasted the partials. Omitted the Comments. Here's the full 'controller'

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
//use DB;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //$posts = Post::all();
        //return Post::where('title', 'Post Two')->get();
        //$posts = DB::select('SELECT * FROM posts');
        //$posts = Post::orderBy('title', 'desc')->take(1)->get();

        $posts = Post::orderBy('title', 'desc')->paginate(10);
        return view('posts.index')->with('posts', $posts);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $post = Post::find($id);
        return view('posts.show')->with('post', $post);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

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

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

@captainburah the comment by @wingly should solve your issue:

@foreach($posts as $post)
    <div class="card card-body mt-3">
        <h3><a href="/posts/{{$post->id}}">{{$post->title}}</h3>
         <small>Written on {{$post->created_at}}</small>
    </div>
@endforeach		
{{$posts->links()}}
2 likes
MichalOravec's avatar

@captainburah How @wingly mentions, just change it to this

@extends('layouts.app')

@section('content')
    <h1>POSTS</h1>

    @if ($posts->isNotEmpty())
        @foreach ($posts as $post)
            <div class="card card-body mt-3">
                <h3><a href="/posts/{{ $post->id }}">{{ $post->title }}</h3>

                <small>Written on {{ $post->created_at }}</small>
            </div>
        @endforeach

        {{ $posts->links() }}
    @else
        <p>No Posts Found</p>
    @endif
@endsection
1 like
wingly's avatar

Make it as i said like that

@extends('layouts.app')

@section('content')
    <h1>POSTS</h1>
    @if(count($posts)>0)
            @foreach($posts as $post)
                <div class="card card-body mt-3">
                    <h3><a href="/posts/{{$post->id}}">{{$post->title}}</h3>
                    <small>Written on {{$post->created_at}}</small>
                </div>
            @endforeach
            {{$posts->links()}}
    @else
        <p>No Posts Found</p>
    @endif

@endsection
1 like
Snapey's avatar

You are overwriting the value of $posts in your foreach loop. since $posts represents a collection of Post, you should use $post as a name for one of them.

@section('content')
    <h1>POSTS</h1>
    @if(count($posts)>0)
            @foreach($posts as $post)
                <div class="card card-body mt-3">
                    <h3><a href="/posts/{{$post->id}}">{{$post->title}}</h3>
                    <small>Written on {{$post->created_at}}</small>
                </div>
            @endforeach
            {{$posts->links()}}
    @else
        <p>No Posts Found</p>
    @endif

@endsection
1 like
CaptainBurah's avatar

@wingly Thanks mate. That was the issue...

Now i get the pagination on the page, except there is no styling, only numbers & arrows... <<12345678910>> Is this bcz i changed my Bootstrap version to v4?

Please or to participate in this conversation.