php - Collection result manipulation with Laravel

290

I am writing a follower module to my site and I got some problems with it. I want to list all the useres but only those ones that are not followed by me. I use this function to get them:

**
     * 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,
        ]);
    }

It works fine with the ids from 0 - 9 when I use without explode but with ids from 10 - ... it kills my Pending scope. The goal would be that add some char into the end of the id (in our case it is ,) and explode it. I did that but it kills my scope from the very begining.

What do you think, what could be possibly wrong? Am I using it absolutely wrong?

Thank you for your answers!

263

Answer

Solution:

Okay, I simply use a foreach before my scope, put every element into an array and it works like a charm!

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

People are also looking for solutions to the problem: php - Uncaught Error: Call to a member function function() on array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.