在Laravel 5.6中更新JSON数据

时间:2018-08-27 06:19:43

标签: json laravel postgresql

问题:需要在地址名称字段中更新json数据。

bookcontroller:

public function edit(Book $book)
{
    return view('books.edit',compact('book'));
}
public function update(Request $request, Book $book)
{
    $book->update($request->all());
    return redirect()->route('books.index')->with('success','Book updated successfully');
}

edit.blade.php

 <form action="{{ route('books.update',$book->id) }}" method="POST">
        @csrf
        @method('PUT')
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    <input type="text" name="name" value="{{ $book->name }}" class="form-control" placeholder="Name">
                </div>
            </div>


            <?php $num=1 ?>

        @foreach($book->address as $value)

                <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Address<?php echo  $num ?> :</strong>
                    <input type="text" name="addr" value="{{ $value['address'] }}" class="form-control" placeholder="Address">
                </div>
            </div>
                <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Country <?php echo  $num ?>:</strong>
                    <input type="text" name="country" value="{{ $value['country']}}" class="form-control" placeholder="Country">
                </div>
            </div>
                <?php  $num++ ?>
            @endforeach
                <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
    </form>

如何更新json数据?

我正在使用postgreSQL .................................................... ................................................... ..........................................谢谢

1 个答案:

答案 0 :(得分:1)

@Kristly D'sena,将输入名称属性“ addr”替换为“ addr []”,并使用数组名称,然后只有这样,您才能在post方法中获得数组格式的多地址,然后将其转换使用 json_encode

将字段寻址到json

在您的edit.blade.php文件中的脚本下方替换。

 @foreach($book->address as $value)
                <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Address<?php echo  $num ?> :</strong>
                    <input type="text" name="addr[]" value="{{ $value['address'] }}" class="form-control" placeholder="Address">
                </div>
            </div>
                <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Country <?php echo  $num ?>:</strong>
                    <input type="text" name="country[]" value="{{ $value['country']}}" class="form-control" placeholder="Country">
                </div>
            </div>
                <?php  $num++ ?>
            @endforeach

在bookcontroller.php文件中更改更新方法

public function update(Request $request, Book $book)
{

    $inputs = $request->all();
    $addr = ($request->addr);
    $country = ($request->country);
    $final_address = array();
    foreach ($addr as $key => $value) {
        $final_address[] = array('address' => $value, 'country'=>$country[$key] );
    }
    $inputs['address'] = json_encode($final_address);        
    $book->update($inputs);

    return redirect()->route('books.index')->with('success','Book updated successfully');
}

这样,您将能够更新地址字段json数据。