【问题标题】:how can i determin that the input is checked in controller?我如何确定输入已在控制器中检查?
【发布时间】:2021-03-09 21:16:10
【问题描述】:

有一个控制器来保存订单,还有一些复选框输入。

我试图解释一下,如果该输入被选中......必须运行一些代码。但在任何条件模式下

$debatorprice$payment$kalaf 返回 null

public function store(Request $request)
{
     if ($request['image']){

         $file = $request['image'];
         $imgname = $file->getClientOriginalName();
         $this->ImageUploader($file, '/upload/orders/');
     }else{
         $imgname=null;
     }

     $payment="";
     $debatorprice="";
        if ($request->has('paymentstatus')){
            $payment=1;
            $debatorprice=0;
        }else{
            $payment=0;
            $totalprice=$request->totalprice;
            $beforeprice=$request->beforeprice;
            $debatorprice=$totalprice-$beforeprice;
        }
        $kalaf='';
        if ($request->has('kalaf')){
            $kalaf=1;
        }else{
            $kalaf=0;
        }

        offlineorder::create([

            'ProductType'=>$request->producttype,
            'OtherProduct'=>$request->otherproduct,
            'CustomerName'=>$request->customername,
            'CorporateName'=>$request->corporatename,
            'Phone'=>$request->phone,
            'Width'=>$request->width,
            'Height'=>$request->height,
            'Model'=>$request->model,
            'Material'=>$request->material,
            'Color'=>$request->color,
            'Count'=>$request->count,
            'Kalaf'=>$request->kalaf,
            'Image'=>$imgname,
            'Text'=>$request->text,
            'Description'=>$request->description,
            'TotalPrice'=>$request->totalprice,
            'BeforePrice'=>$request->beforeprice,
            'PaymentStatus'=>$payment,
            'DebtorPrice'=>$debatorprice,
            'SKU'=>$request->sku,
            'ChapStatus'=>0,
            'DesignStatus'=>0,
            'View'=>0,
            'Link'=>0,

        ]);

        session()->flash('add_order','سفارش شما با موفقیت ثبت شد');
        return back();
    }

我尝试了if ($request->paymentstatus==null),但也没有用

【问题讨论】:

  • 一方面,您没有在'Kalaf'=>$request->kalaf, 中使用$kalaf。你的复选框有值吗?你dd($request) 看看它们是否真的在请求中发送了吗?!
  • 尝试使用 isset($request->paymentstatus) 代替空条件

标签: php laravel if-statement checkbox laravel-8


【解决方案1】:

你可以使用

$request->filled('input')

这将检查输入是否存在并填充。

如果您只想检查输入是否存在,您可以使用

$request->has('input')

来源:https://laravel.com/docs/8.x/requests#determining-if-input-is-present

【讨论】:

    【解决方案2】:

    用 empty() 方法检查 它会检查空以及如果 isset

    if(empty($request->paymentstatus)){}
    

    【讨论】:

      【解决方案3】:

      使用 isset 函数检查如下:

      if(!isset($request->paymentstatus)){}
      

      【讨论】:

        猜你喜欢
        • 2017-08-22
        • 2014-12-24
        • 1970-01-01
        • 2010-09-16
        • 2010-12-19
        • 2018-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多