0
votes

I'm trying to paginate data that's returned from a model.

But keep getting an error :

Call to undefined method App\Shortlist::links()

Its on my Shortlist model and the code I'm using the paginate is (Controller)...

 public function shortlist()
    {
        $view_data = array
        (
            'shortlist' => Shortlist::where('user_id', $this->user_id)->with('property')->orderBy('created_at', 'desc')->paginate(4)
        );

        $view = 'frontend.'.themeOptions().'.account.shortlist';

        // Change View Per Template...
        if(view()->exists($view))
        {
            // Shared View in the Templates...
            return view($view,  $view_data);
        }
        else
        {
            // Load Shared View...
            return view('frontend.shared.account.shortlist', $view_data);
        }
    }

Then in my view, I'm using :

{{ $shortlist->links() }}

But keep getting that error, any help as to why?

Thanks

1

1 Answers

1
votes

$shortlist is not defined, you need to return that collection to your view.

You can call ->links() on a collection. $view_data is just an array, you want to access the shortlist collection in that array, so you need to call it like this

{{ $view_data['shortlist']->links() }}

Further why do you create this variable:

$view_data = array ( 'shortlist' => Shortlist::where('user_id', $this->user_id)->with('property')->orderBy('created_at', 'desc')->paginate(4) );

if you don't use it. If you don't use it I would just declare it like this:

$shortlist = Shortlist::where('user_id', $this->user_id)->with('property')->orderBy('created_at', 'desc')->paginate(4);

and return that to your view, looks cleaner.