2

i did query from db and return array

$games = Game::where('admin_id', $user->id)->where('active', true)->get();

now i am trying to add object inside $games array like this

$games->push(['name' => 'Game1', 'color' => 'red']); //its adding array instead object

please explain Thank you

1 Answer 1

3

Because you pushed an array, so it is adding array.

// here, you are pushing the array so you get the array.
['name' => 'Game1', 'color' => 'red']

Pushing the object like this:

$games = $games->push(new Game(['name' => 'Game1', 'color' => 'red']));

or this way:

$games = $games->push((object)['name' => 'Game1', 'color' => 'red']);
Sign up to request clarification or add additional context in comments.

6 Comments

i don't want to save to db i just want to add inside array variable
@sidheart there is nothing related saving into db? where did you get it from?
@sidheart using eloquent model does not mean saving into db.
That is the problem i am facing that i have collection get from db and i want to add 2 more object inside array variable that i can use it like $game->name later
@sidheart my answer did exactly what you wanted, the two solutions I provided are not updating db at all. I do not understand what are you worrying about.
|

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.