【问题标题】:Display JSON Array in laravel blade在 laravel 刀片中显示 JSON 数组
【发布时间】:2021-02-14 22:17:31
【问题描述】:

我需要在输入框中单独显示列extras_name 内的 JSON 数据,并在编辑时填充值

这是刀片文件代码

<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>

这是控制器编辑功能

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

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

以下是数据在数据库中的保存方式

【问题讨论】:

    标签: javascript php arrays json laravel


    【解决方案1】:

    首先json_decode这个
    然后在foreach 你可以使用它。
    刀片文件:

    @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
    

    【讨论】:

      【解决方案2】:

      这样做没有意义 相反,您可以在输入下方的列表项上显示您的值,并在添加或删除新值时使用 ajax 刷新列表

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-22
        • 1970-01-01
        • 2014-09-14
        • 1970-01-01
        • 1970-01-01
        • 2020-10-10
        • 2020-04-10
        • 2019-02-22
        相关资源
        最近更新 更多