raziel79's avatar

How to get value from array in blade view

Hi all,

in my controller i have $user->posts

return an array of max 7 posts.

in my view i have 7 text field with name attribute post[] and i need to display all 7 textfield in edit form filled with right post value:

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

so , how can i fill value attribute with relative post[$i] value passed to my view?

i tried to use ($user->post)[$i] but won't work, assume that my view correctly receive my $post array.

any ideas?

0 likes
12 replies
EventFellows's avatar

Can you format the code so it gets readable with 3 backticks `, please

Instruction link below this input field.

raziel79's avatar

ok code formatted :) thx , as you can see, i need to create 7 text field but fill it with relative post value

post is passed to view with $user->post , it is an array of items with 0 to 6 index value but i cannot display in for loop and blade view

any help ?

raziel79's avatar

So i have found the problem:

if i use

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

i display in all field the value in 0 key of array

but if I replace [0] with [$i] i give an error

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

the problem is $i inside loop, how to fix?

cviv's avatar
@for ($i = 0; $i < 7; $i++)
<input type="text" name="post[]" value="{{$post[$i]}}" class="form-control" placeholder="Post n.{{ $i+1 }}" 
@endfor

you need to pass $post from your controller

raziel79's avatar

mhhh the user object have a property "post" that is an array.

why do i pass "post" from my controller?

in my controller i have

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

the problem is indexing inside the loop...

EventFellows's avatar

you should be able to use @cviv suggesstion but use $user->post instead of $post

You can also make it a @foreach loop. That would be much cleaner in my opinion and also more flexible if it becomes a different array size in future.

raziel79's avatar

ok but isn't there a better method?

Suppose that i need to add some others property array like in my model.

I need to edit my return value in UserController@edit and my View file times and times, i don't think is a good idea.

The best solution for me would be to inject my $user object and not also singles property of the same object that are already inside it. Immagine that your model has dozen of array like properties, my return statements will be very complex and long, not good.

If i cannot indexing in a for loop my array like property for me is a strange behaviour for a template engine. Assuming that i cannot use foreach.

what about this?

KamalKhan's avatar

This should do the trick:

@foreach($user->posts as $post)
    <input type="text" name="post[{{ $post->id }}]" value="{{ $post->value }}" class="form-control" placeholder="Post n.{{ $loop->iteration }}">
@endforeach
raziel79's avatar

Nice but i must create 7 post text field with this i create only a number of text box as post items

KamalKhan's avatar

It will create as many post text fields the user has. So if $user->posts->count() == 9999, it will create 9999 text fields.

KamalKhan's avatar

If you want 7 textfields related to each post, you may add 7 fields instead of 1. Like so:

@foreach($user->posts as $post)
    
    <input type="text" name="post[{{ $post->id }}][field1]" value="{{ $post->field1 }}" class="form-control" placeholder="Post n.{{ $loop->iteration }}">

    <input type="text" name="post[{{ $post->id }}][field2]" value="{{ $post->field2 }}" class="form-control" placeholder="Post n.{{ $loop->iteration }}">

    <input type="text" name="post[{{ $post->id }}][field3]" value="{{ $post->field3 }}" class="form-control" placeholder="Post n.{{ $loop->iteration }}">

    <input type="text" name="post[{{ $post->id }}][field4]" value="{{ $post->field4 }}" class="form-control" placeholder="Post n.{{ $loop->iteration }}">

    <input type="text" name="post[{{ $post->id }}][field5]" value="{{ $post->field5 }}" class="form-control" placeholder="Post n.{{ $loop->iteration }}">

    <input type="text" name="post[{{ $post->id }}][field6]" value="{{ $post->field6 }}" class="form-control" placeholder="Post n.{{ $loop->iteration }}">

    <input type="text" name="post[{{ $post->id }}][field7]" value="{{ $post->field7 }}" class="form-control" placeholder="Post n.{{ $loop->iteration }}">
@endforeach
raziel79's avatar
raziel79
OP
Best Answer
Level 7

fyi

the error was in $user->post, if i have only 2 posts and 7 field after second loop i need to test if $user->post[$i] exists, here better solution, quite simple:

thx all

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

Please or to participate in this conversation.