在Laravel 5.4中,请求验证仅适用于分页的第一页

问题描述 投票:0回答:1

所以正在发生的事情是我为用户帖子和用户朋友发布了一系列id,然后检查当前页面的帖子ID是否在id数组中。这是我想要的第一页,但因为我使用分页它不适用于第一页之后的页面。

public function authorize(Request $request)
{
    $postRepo = $this->postRepo;
    $posts = $postRepo->index();
    $posts->all();
    $post_id = $request->route('post_id');
    $posts = $posts->pluck('id')->toArray();
    if (in_array($post_id, $posts)) {
       return true;
    }
}

目前,id的数组只显示第一页似乎是问题的那些

array:4 [▼
0 => 10
1 => 11
2 => 9
3 => 17
]
php laravel
1个回答
0
投票

您可以将authorize()方法更改为:

public function authorize(Request $request)
{
    $postRepo = $this->postRepo;
    $post_id = $request->route('post_id');

    // Here I believe that index() method return a Eloquent Builder
    $posts = $postRepo->index();

    return (boolean) $posts->where('id', $post_id)->count();
}

如果在$postRepo->index()中存在$ post_id,它将返回true。

© www.soinside.com 2019 - 2024. All rights reserved.