【问题标题】:Request for the value rather than key/id in Laravel controller在 Laravel 控制器中请求值而不是键/ID
【发布时间】:2020-11-13 04:57:19
【问题描述】:

我正在使用来自控制器的json_encode($items); 函数(特别是它的 id 的“价格”字段)从数据库获取数据到发票视图页面,它运行良好。但是当我将该特定字段插入数据库时​​,只存储 id/key 而不是实际值。任何猜测如何在控制器中请求值而不是键/ID。

这里是控制器功能:

public function getPurchaseItems($id) 
   {
       $items = DB::table("purchase")->where("pur_id",$id)->pluck("price","pur_id");
       return json_encode($items);
    
   }

这里是html代码:

<div class="form-group col-md-1">
      <label for="in">Price</label>
      <select name="price" readonly class="form-control prc" id="price">        
      </select>
    </div> 

这是脚本:

            jQuery('select[name="state"]').on('change',function(){
              
               var stateID = jQuery(this).val();
               if(stateID)
               {
                  jQuery.ajax({
                     url : 'invoice/getPurchaseItems/' +stateID,
                     type : "GET",
                     dataType : "json",
                     success:function(data)
                     {
                      
                        console.log(data);
                        jQuery('select[name="price"]').empty();
                        jQuery.each(data, function(key,value){
                           $('select[name="price"]').append('<option value="'+ key +'">'+ value +'</option>');
                        });
                  
                     }
                  });
               }
               else
               {
               
                
                $('select[name="price"]').empty();
                
                  
               }
            });

这是请求的数据并存储到数据库:

public function insertInv(Request $request){
       $price = $request->input('price');

        $invoiceId= DB::table('invoice')->insertGetId($invo);     
        $invo_det=array('invoice_no'=>$invoiceId,'price'=>$price);

         DB::table('invoice_details')->insert($invo_det);

【问题讨论】:

    标签: javascript php json laravel


    【解决方案1】:

    修改你的 insertInv 函数

    $price = $request-&gt;input('price'); 中删除 input() 或dd($request-&gt;all()); 并检查控制器中的所有值是否格式正确并在此处发布结果

    【讨论】:

    • 如果我删除它,用什么方法代替 input()?
    • array:22 [▼ "_token" =&gt; "JwlkRyfXpf4pQc32QeGppVZwmWZBj9SYfBOQAA0p" "customer_name" =&gt; "John Doe" "c_email" =&gt; "john@gmail.com" "c_phone" =&gt; "12232" "billing_address" =&gt; "aaa" "term" =&gt; "23" "order_date" =&gt; "2020-07-09" "due_date" =&gt; "2020-07-15" "country" =&gt; "5" "state" =&gt; "44" "qty" =&gt; "44" "unit" =&gt; "44" "price" =&gt; "44" "cqty" =&gt; "23" "sub_total" =&gt; "232323" "gst" =&gt; null "discount" =&gt; "20" "net_total" =&gt; "2323232" "paid" =&gt; "2323" "due" =&gt; "2323" "payment_type" =&gt; "نقد" "invoice_msg" =&gt; "ssdfd" ]
    • Nthing 只需使用 $price = $request->price;就这么简单,在定义变量 dd($price);并检查价格是否正在打印,如果正在打印,则一切正常。
    • 打印为空
    • 给我anydesk id 我会修复它
    猜你喜欢
    • 2021-12-26
    • 2017-02-26
    • 2021-05-16
    • 1970-01-01
    • 2018-03-05
    • 2015-03-04
    • 2020-06-16
    • 2020-04-01
    • 2019-02-25
    相关资源
    最近更新 更多