Jareer
Jareer

Reputation: 252

Display JSON Array in laravel blade

I need to display the JSON data which is inside the column extras_name individually inside a input boxes with the value filled on edit

enter image description here

Here's the blade file codes

<div class="card">
        <h5 class="card-header">
            Manage Extras
        </h5>
        <div class="card-body">
            {{ $customProduct }}
            <br><br><br>

            <div class="optionBox">
                <div class="block main">
                    <div class="form-row pb-2">
                        <div class="col">
                          <input type="text" class="form-control" name="extras_name[]" value="{{ $customProduct->name }}" placeholder="Name">
                        </div>
                        <div class="col">
                          <input type="text" class="form-control" name="extras_price[]" placeholder="Price">
                        </div>
                        <div class="col">
                            <button type="button" class="remove btn btn-danger"><i class="fas fa-trash-alt"></i></button>
                        </div>
                    </div>
                    <div class="block mt-2 ">
                        <button type="button" class="add btn btn-success">Add Extras</i></button>
                    </div>
                </div>
            </div>
        </div>
    </div>

Here's the Controller Edit function

public function edit($venue, CustomProduct $customProduct)
    {     
        $data = [     
            'customProduct'=>$customProduct,
        ];

        return view('manage.custom-products.edit')->with($data);
    }

Here's how the data is saved in the database

enter image description here

Upvotes: 2

Views: 1637

Answers (3)

Meheraz
Meheraz

Reputation: 39

If you want to show data in a table then you can use this code

<td>

 @foreach(json_decode($value->extras_name, true) as $extras_name)

     {{ $extras_name}}

   @endforeach
</td>

Upvotes: 1

milad hedayatpoor
milad hedayatpoor

Reputation: 636

it doesnt make sense to do that instead you can show your values on list item below your input and when new value is added or removed refresh the list using ajax

Upvotes: 0

Reza Sheykhi
Reza Sheykhi

Reputation: 797

first of all json_decode this
and then in foreach you can use that.
Blade File:

@if(!empty(json_decode($data->extras_name))) {{--JUST FOR CHEKING THAT IF THIS WAS NOT JSON OR THIS WAS NULL, THE ERROR NOT SHOWING--}}
    @foreach(json_decode($data->extras_name as $title=>$value))
        <label>
            {{$title ?? ''}} : {{$value ?? ''}}
        </label>
        <br/>
    @endforeach
@endif

Upvotes: 0

Related Questions