【问题标题】:How to use laravel pagination with filter如何使用带有过滤器的laravel分页
【发布时间】:2019-03-01 06:36:36
【问题描述】:

我在帖子模型中有getCanSeeAttribute 功能,我尝试使用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;
        });

问题是当我使用过滤器时,它只获取数据属性,但我需要所有分页属性。

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

can_see not column in table it is Accessor

【问题讨论】:

    标签: database laravel laravel-5 eloquent laravel-pagination


    【解决方案1】:

    首先,我希望你知道你在做什么。假设您需要获得将 can_see 字段设置为 true 的结果,您应该使用:

    $posts = Post::where('can_see', true)
             ->where(function($q) {
                $q->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);
    

    如您所见,我在额外的 where 闭包中额外包装了(where .. orWhere),以确保生成有效的查询。

    否则你应该使用:

    $posts = Post::where(function($q) {
                $q->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);
    
    $posts = $posts->setCollection($posts->getCollection()->filter(function($post){
            return $post->can_see == true;
       })
    );
    

    但是,在您的情况下,第二种方式不太可能更好。假设您有 100 万条匹配记录,然后其中的 can_see 设置为 true,其余的设置为 false,这将导致您将从数据库中获取 100 万条记录,然后您将只过滤其中的 10它们会成为您应用程序的性能杀手。

    【讨论】:

    • can_see 不是帖子表中的列,所以我使用 filter()
    • 好的,所以这可能是用例,但是假设您将有很多记录会导致性能问题,因此如果它基于某些权限,您可能应该将它们移动到数据库以获取帖子用户拥有来自数据库的权限,无需在 PHP 端进行过滤。
    • 但在我的情况下“can_see”基于 (friends , follower , all) ,所以有没有办法将权限放入数据库中
    • @hossamGamal 所以在这种情况下,你可以使用我展示的第二个解决方案
    【解决方案2】:

    您可以添加以下代码,在$post之后定义

    $post->appends($request->only('posted_type'));
    

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 1970-01-01
      • 2022-07-11
      • 1970-01-01
      • 2021-05-04
      • 2021-02-28
      • 1970-01-01
      • 2018-06-02
      • 2018-07-10
      相关资源
      最近更新 更多