1

Laravel 5.3 prjects: i have e for loop and an object passed from my controller with property as array

My UserController@edit

return view('user.edit',compact('user'));

My user object have a array like property "Post" so $user->post return an array like

[0]=>'post1',
[1]=>'post2',
... more ...

in my view with BLADE i need to display 7 text field input box as array and fill it with array right value of $user->post property:

 @for ($i = 0; $i < 7; $i++)
        <input type="text" name="ingredients[]" value="{{ ($user->post)[$i] }}" class="form-control" placeholder="Post title">
    @endfor

this return error:

ErrorException in 600ad3d79a7e4216538932fc71b893314cf18166.php line 65:
Undefined offset: 2

the problem is the $i inside value attribute if i replace with 0 or 1 (hardcoded index value) it work but with loop index $i it fails??

any ideas how to indexing array property in a for loop on blade template engine?

note: i cannot user foreach loop.

thx alll

2
  • What's the exact output of dd($user->post); ? Commented Feb 10, 2017 at 11:49
  • [0=>'Post1',1=>'Post2'] Commented Feb 10, 2017 at 13:24

1 Answer 1

2

You dont appear to be limiting your loop to only what actually exists in the array, so make the for loop stop at the max size of the array

@for ($i = 0; $i < count($user->post); $i++)
    <input type="text" name="ingredients[]" value="{{ $user->post[$i] }}" class="form-control" placeholder="Post title">
@endfor
Sign up to request clarification or add additional context in comments.

1 Comment

Thx RiggsFolly nice, you save my life!

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.