Laravel eloquent pluck data before pagination

Viewed 529

here is the paginate query:

User::where('username', 'test')
      ->with('followers.follower_details')
      ->paginate(25)

but i want something to pluck data before pagination like this:

User::where('username', 'test')
          ->with('followers.follower_details')
          ->pluck('followers')
          ->paginate(25) 

could not find anything useful by googling, any tricks to handle these type of queries?

Updated i tried the KurtFriars's way it works but the problem with data is

Follower::where('following',
        User::query()->where('username', request()->username)
            ->firstOrFail()->id)->with('follower_details')->paginate(25)

the response is:

{
        "current_page": 1,
        "data": [
            {
                "id": 1,
                "user_id": 1,
                "following": 2,
                "created_at": null,
                "updated_at": null,
                "follower_details": {
                    "name": "test test2",
                    "username": "test",
                    "email": "test@gmail.com"
                }
            },
            {
                "id": 5,
                "user_id": 3,
                "following": 2,
                "created_at": null,
                "updated_at": null,
                "follower_details": {
                    "name": "test2",
                    "username": "test2",
                    "email": "test2@gmail.com",
                }
            }
        ],
        .... // paginations links 
    }

but i just need follower_details, like this:

{
        "current_page": 1,
        "data": [
            {

                    "name": "test test2",
                    "username": "test",
                    "email": "test@gmail.com"
                
            },
            {
                    "name": "test2",
                    "username": "test2",
                    "email": "test2@gmail.com",
                
            }
        ],
        "first_page_url": "http://localhost/api/user/nod/followers?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://localhost/api/user/nod/followers?page=1",
        "next_page_url": null,
        "path": "http://localhost/api/user/nod/followers",
        "per_page": 5,
        "prev_page_url": null,
        "to": 2,
        "total": 2
    }
1 Answers

What about breaking it down like so

// this will pluck this user followers ids to array
$user_followers = User::where('username', 'username')->first()->followers()->pluck('id')->toArray(); 

// this will get follower details from user follower's array of ids
$follower_details = FollowerDetails::whereIn('following', $user_followers)->paginate(10);

Related