Laravel-5.8爆炸无法爆炸

时间:2019-06-23 11:09:52

标签: php laravel-5.8

我确实保存了implode之类的计算机字段。

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

implode

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

我创建了多个复选框,并存储了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>
           <input type="submit" class="btn btn-primary" value="Save">
</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)

此代码将无效,因为computer字段是一个字符串,而$request('computer')是一个数组。

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

此外,就像我在上面的评论一样,您必须传递csrf_token才能提交表单。因此,在表单标签中使用@csrf

尝试下面的代码,我对每一步都进行了注释,以便更好地理解。

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

 //Now fetch the user's computer record, it will give you a string.

   $users_computers = Order::where('user_id',auth()->id())->get('computer');

 //convert this string to array

   $users_computers = explode(',',$users_computers);

 //Now check if there is at least one order is common or exists 

 $common_orders = array_intersect($users_computers, $request['computer']);

 //throw error if any orders exists already

 if(count($common_orders)>0)
   {
      alert()->error('Warning', 'You ordered already');
      return redirect()->back();
      //To give which computers user have orders you can access $common_orders values.
      }

$order = new Order();
$order->user_id = auth()->user()->id;
$order->description = $request->description;
$computer = implode(",", $request->computer);
$order->computer = $computer;
$order->save();
return redirect()->back();//pass here a success message if you want
}

使用雄辩的关系来更方便地获取和存储用户的订单。