Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Anthonynzube's avatar

Problems with Arrays

I'm updating a todo list and there are two tables that are involved in updating any todo. The todos table and a steps table. I've already created the relationships. And to update a todo, I have an update function that looks like this

 public function update(TodoCreateRequest $request, Todo $todo)
    {
        $todo->update([
            'title' => $request->title,
            'description' => $request->description,
        ]);

        $arrSize = sizeof($request->step);

        for ($i=0; $i<=$arrSize; $i++) { 
            return "hello";
        }
        
    }

Here lies the problem. When i do this

dd($request->step);

i get this result

array:3 [▼
  0 => "Don't do anything stupid"
  1 => "Don't leave your isolation chamber"
  2 => "Don't go outside"
]

Also when i try to get the size of $request->step by doing this

sizeof($request->step);

it gives me

3

but when i run the update function it only returns

hello once

instead of returning hello more than once according to the condition in the for loop.

0 likes
2 replies
Braunson's avatar

It's displaying hello once because your using return. The return ends the loop. Instead try echo 'hello';

1 like
Anthonynzube's avatar

That worked but that was just a test to see if i had a problem. But the main problem is when i do this

 public function update(TodoCreateRequest $request, Todo $todo)
    {
        $todo->update([
            'title' => $request->title,
            'description' => $request->description,
        ]);

        if ($request->step) {
            foreach ($request->step as $step) {
                $todo->steps()->update(['name' => $step]);
            }
        }
        
    }

instead of updating the steps with

  0 => "Don't do anything stupid"
  1 => "Don't leave your isolation chamber"
  2 => "Don't go outside"

i get this saved to my database

 0 => "Don't do anything stupid"
  1 => "Don't do anything stupid"
  2 => "Don't do anything stupid"

Please or to participate in this conversation.