如何将checkvboxes的值设置为json

时间:2019-06-22 22:08:54

标签: laravel-5.8

用户对订单的出价不能超过一次。如果用户再次单击该订单,则应该在响应页面上提示他,提示用户先前已经对该订单进行了出价。

  

我没有做json

如何在json中保存值复选框。

我的数据保存如下:

include

我创建了多个复选框,并存储了implode台计算机

order.blade.php

<form action="{{ route('store') }}" method="post">
    <input type="checkbox" name="computer[]" value="1" id="hp">
    <label for="hp">HP</label>
    <input type="checkbox" name="computer[]" value="2" id="dell">
    <label for="dell">DELL</label>
    <input type="checkbox" name="computer[]" value="3" id="asus">
    <label for="asus">ASUS</label>
    <input type="checkbox" name="computer[]" value="4" id="acer">
    <label for="acer">ACER</label>
    <input type="checkbox" name="computer[]" value="5" id="sony">
    <label for="sony">Sony</label>
    <input type="checkbox" name="computer[]" value="6" id="fujitsu">
    <label for="fujitsu">Fujitsu</label>
    <input type="checkbox" name="computer[]" value="other_barnds" id="other_barnds">
    <label for="other_barnds">Other</label>
</form>

OrderController.php

public function store(Request $request)
{
    $this->validate(request(), [
        'computer' => 'required'
    ]);

    $order = Order::where('user_id',auth()->id())
        ->where('computer', request('computer'))
        ->exists(); 

    $order = new Order($request->all());
    $order->user_id = auth()->user()->id;
    $order->description = $request->description;
    $computer = implode(",", $request->computer);
    $order->computer = $computer;
    $order->save();

    if ($order) {
        alert()->error('Warning', 'You ordered already');
        return redirect()->back();
    }
}

1 个答案:

答案 0 :(得分:0)

在OrderController.php中进行如下更改:

    $order = new Order($request->all());
    $order->user_id = auth()->user()->id;
    $order->description = $request->description;
    // json encoding (use array_values if u need values only)
    $computer = json_encode($request->computer);
    $order->computer = $computer;
    $order->save();