0

I'm developing a web application in Laravel. The below code is not from the application but gives you a simple idea of the problem I am having.

The following code works

public $my_users = [];  //This is outside function

$users = [];
$users = User::all();
$this->my_users = $users;

That's pretty simple, works fine. However, i want to be able to loop through all of the users and add ones of my choosing like so:

public $my_users = [];  //This is outside function

$users = [];
$all_users = User::all();
foreach ($all_users as $user)
{
    array_push($users, $user);
}
$this->my_users = $users;

Again, what I am doing here has no logic behind it, it's just an example.

In this case both pieces of code should have the same result. However, the second piece of code isn't working. It seems to be the array_push function that isn't working here. is there any reason why this is? What am I to do here?

3
  • Whats the result of var_dump($users); ? Commented Mar 2, 2017 at 6:02
  • It seems like you are looping through an array to create another array and then assigning in the global array. Why you are doing that? Instead can't you directly assign the array? Commented Mar 2, 2017 at 6:04
  • I feel like there's something missing here. There's no part of either snippets that should be failing unless the users table is empty (in which case the first snippet will have an object and the second one an empty array). Commented Mar 2, 2017 at 6:48

4 Answers 4

3

User::all(); returns a type collection (Illuminate\Database\Eloquent\Collection). So first block of code you delcared an array $users = []; and immediately override it with:

$this->my_users = User::all();

So you're basically assigning a collection to $this->my_users.

2nd block of code you are looping through the collection and assign it to the array.

$this->my_users = [
   //instance of App\User 1,
   //instance of App\User 2 etc
];

Try dd the result and you should be able to see a clearer picture.

Sign up to request clarification or add additional context in comments.

4 Comments

So what I need to do is create an instance of a collection and then add to that collection. Do you know how to do this? I've tried $users = new \Illuminate\Database\Eloquent\Collection; didn't work.
You can just add it to a normal array and convert it to collection by doing collect($usersArray)
I don't understand what you mean. I have a collection all_users, how do I create another collection so I can add specific users from the all_users collection to it? I can't add instances of all_users to an array.
Any reason you would want to add a collection into another collection? Anyway you can do $newCollection = collect(); to create a new collection. Then you can use $newCollection->push($all_users);
1

Laravel's query results are always a collection so if filtering is what you really need then do:

$users = Users::all()->filter(function ($u) { /* conditions to accept the user */ });

Comments

0

You could use the default helper of the Laravel instead of array_push

array_add()

See the documentation here: https://laravel.com/docs/5.4/helpers#method-array-add

Comments

-1

$users.push($user); i think this is how you push into your array

2 Comments

It's not though
my bad. that's how you do it in javascript but not in laravel i guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.