'Laravel array value get to show one by one
My database
My blade view
But I want to have this
My controller
$profiles= Profile::where('id', $id)->get();
return view('artists.profile_edit',compact('profiles'));
My blade template
@foreach ($profiles as $key => $profile)
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_name') ? 'is-invalid' : '' }}" name="social_media_channel_name[]" value="{{ $profile['social_media_channel_name'] }}" placeholder="Social Media Channel Name">
</div>
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_link') ? 'is-invalid' : '' }}" name="social_media_channel_link[]" value="{{ $profile['social_media_channel_link'] }}" placeholder="Social Media Channel Link">
</div>
@endforeach
Here I am trying to get array data and show in blade one by one.
Solution 1:[1]
First of all, I think your database needs to be redesigned, because currently it's possible to have the array of names and the array of links a different size, which doesn't make sense.
But using this design, you'll need your "for" loop to loop by number instead by "foreach". Get the minimum array length of names and links, and then loop through those numbers.
Solution 2:[2]
Update 1.0
I thinks that these columns from json
or longtext
MySQL type So, you need to use json_decode method to convert this string into array like so
@foreach (json_decode($profiles['social_media_channel_name']) as $profile)
{{-- `$loop` is a variable that comes with `@for`, `@foreach`, and `@forelse` --}}
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_name') ? 'is-invalid' : '' }}" name="social_media_channel_name[]" value="{{ json_decode($profiles['social_media_channel_name'])[$loop->index] }}" placeholder="Social Media Channel Name">
</div>
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_link') ? 'is-invalid' : '' }}" name="social_media_channel_link[]" value="{{ json_decode($profiles['social_media_channel_link'])[$loop->index] }}" placeholder="Social Media Channel Link">
</div>
@endforeach
Well, according to your last comment, this means that $profiles['social_media_channel_name']
from type array
So, your code must be like so
@foreach ($profiles['social_media_channel_name'] as $profile)
{{-- `$loop` is a variable that comes with `@for`, `@foreach`, and `@forelse` --}}
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_name') ? 'is-invalid' : '' }}" name="social_media_channel_name[]" value="{{ json_decode($profiles['social_media_channel_name'])[$loop->index] }}" placeholder="Social Media Channel Name">
</div>
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_link') ? 'is-invalid' : '' }}" name="social_media_channel_link[]" value="{{ json_decode($profiles['social_media_channel_link'])[$loop->index] }}" placeholder="Social Media Channel Link">
</div>
@endforeach
You can read more alse about The Loop Variable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Magmatic |
Solution 2 |