mardi 25 septembre 2018

How to use laravel pagination with filter

I have getCanSeeAttribute function in post model, and I try to get posts with pagination using filter()

$posts = Post::where(function ($query1) use ($users) {
            $query1->where('posted_type', 'user')->whereIn('posted_id', $users);
        })
        ->orWhere(function ($query2) use ($stores) {
            $query2->where('posted_type', 'store')->whereIn('posted_id', $stores);
        })
        ->with('likes', 'postable')
        ->withCount('comments', 'likes')
        ->latest()->paginate($paginate)->filter(function($post){
            return $post->can_see == true;
        });

The problem is when I use filter, it gets data attribute only, but I need all pagination attributes.

first_page_url": "http://localhost:8000/api/timeline?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://localhost:8000/api/timeline?page=1",
"next_page_url": null,
"path": "http://localhost:8000/api/timeline",
"per_page": 10,
"prev_page_url": null,
"to": 6,
"total": 6
enter code here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire