【问题标题】:How to put value of checkvboxes to set json如何将复选框的值设置为 json
【发布时间】:2019-11-05 06:05:50
【问题描述】:

一个用户不能对一个订单多次出价。如果用户再次点击该订单,他应该会收到一个响应页面,提示用户之前曾对该订单出价。

我没有做json

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

我的数据保存如下:

我创建了多个复选框并存储了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();
    }
}

【问题讨论】:

    标签: laravel-5.8


    【解决方案1】:

    在 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-16
      • 2014-09-20
      • 1970-01-01
      • 2020-06-25
      • 2016-09-19
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多