使用Laravel进行收集结果处理

时间:2016-06-29 10:45:51

标签: php laravel collections eloquent

我正在为我的网站编写一个关注模块,我遇到了一些问题。我想列出所有的用途,但只列出那些我没有遵循的用途。 我使用这个函数来获取它们:

**
     * Listing users
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @todo Valamiért hat kétjegyű az követendő user id akkor a scope lehal
     */
    public function listUsers() {

        //Declare an empty array for the result of the collection
        $ids = array();

        $collection = $this->user->followers;
        $collection->each(function($follower) use (&$ids) {
            $ids[] = explode(',', $follower->id . ',');
        });


        $users = User::Pending($ids)->get();

        dd($users);

        return view('profile.listUsers', [
            'users' => $users,
            '_user' => $this->user,
        ]);
    }

当我使用时没有爆炸但是使用10的ids时,它可以正常使用0到9的ids - 它会杀死我的待定范围。 目标是在id的末尾添加一些char(在我们的例子中是),并将其爆炸。我做到了,但它从一开始就杀死了我的范围。

您如何看待,可能出现什么问题?我用它绝对错了吗?

感谢您的回答!

1 个答案:

答案 0 :(得分:0)

好吧,我只是在我的范围之前使用foreach,将每个元素放入一个数组中,它就像一个魅力!

foreach($collection as $follower) {
            $ids[] = $follower->id;
        }