eleven0's avatar

Pagination on a Custom Function of a Model

Hello all, I have a simple function (see below) in a model, i render content based on who the user is.

    public function customcontent($userid)
    {
        $user = User::where('id', $userid)->sole();
        if ($user->xmuser() == true) {
            return $this->customdocs->sortByDesc('created_at');
        }
        if (count($user->xonly) > 0) {
            $docs = Doc::where('userid', $user->id)->get();
            return $docs->sortByDesc('created_at');
        }
    }

i can't seem to apply paginate() on these. My second issue is how I would implement links for pagination in blade file, as my content is rendered for each user. Do links accept custom variable as I have it?

0 likes
4 replies
Sinnbeck's avatar

You need to make sure you actually load then from the database as paginated

 public function customcontent($userid)
    {
        $user = User::where('id', $userid)->sole();
        if ($user->xmuser() == true) {
            return $this->customdocs()->latest()->paginate();
        }
        if (count($user->xonly) > 0) {
            return Doc::where('userid', $user->id)->latest()->paginate();
        }
    }
eleven0's avatar

@Sinnbeck thanks. Could you also help me display the links for it? I get following error, when I do below.

{{ $co->customcontent(Auth::user()->id)->links() }}

this throws BadMethodCallException Method Illuminate\Database\Eloquent\Collection::links does not exist.

eleven0's avatar

@Sinnbeck after. when i dd;

{{ dd($co->customcontent(Auth::user()->id)) }}

I actually get a collection with 3 arrays (i am using paginate(1) for testing of my sample date) as below;

Illuminate\Database\Eloquent\Collection {#990 ▼
  #items: array:3 [▶]
  #escapeWhenCastingToString: false
}

Please or to participate in this conversation.