分页前 Laravel 雄辩的采摘数据

Posted

技术标签:

【中文标题】分页前 Laravel 雄辩的采摘数据【英文标题】:Laravel eloquent pluck data before pagination 【发布时间】:2020-11-25 15:28:21 【问题描述】:

这里是分页查询:

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

但我想要在分页之前提取数据,如下所示:

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

通过谷歌搜索找不到任何有用的东西,有什么技巧可以处理这些类型的查询吗?

更新 我尝试了 KurtFriars 的工作方式,但数据的问题是

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

回复是:


        "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 
    

但我只需要follower_details,像这样:


        "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
    

【问题讨论】:

为什么要在分页前采摘?还有一件事,如果您不需要follower_details,那您为什么要急切地加载它? @BasheerKharoti 因为我不需要属于 followersfollowers.follower_details 的数据,并且为了简单起见 所以你只需要follower_details数据? @BasheerKharoti 是的 【参考方案1】:

把它分解成这样怎么样

// 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);

【讨论】:

以上是关于分页前 Laravel 雄辩的采摘数据的主要内容,如果未能解决你的问题,请参考以下文章

关系数据库中的 Laravel 雄辩分页调用

带有雄辩查询的 Laravel 分页

Laravel Livewire 无法解析模板上分页的雄辩关系

larave Elasticsearch scout Unsupported operand types报错

larave Elasticsearch scout Unsupported operand types报错

Laravel orderBy 与分页和关系