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 >
<input type="text" name="social_media_channel_name[]" value="{{ $profile['social_media_channel_name'] }}" placeholder="Social Media Channel Name">
</div>
<div >
<input type="text" 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.
CodePudding user response:
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.
CodePudding user response:
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 >
<input type="text" name="social_media_channel_name[]" value="{{ json_decode($profiles['social_media_channel_name'])[$loop->index] }}" placeholder="Social Media Channel Name">
</div>
<div >
<input type="text" 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