简体   繁体   English

请求 Laravel controller 中的值而不是键/id

[英]Request for the value rather than key/id in Laravel controller

I'm fetching data from database to the invoice view page using json_encode($items);我正在使用json_encode($items);从数据库中获取数据到发票视图页面。 function (particularly 'price' field by it's id) from controller, it works well. controller 中的 function(特别是 'price' 字段的 id),它运行良好。 But when I insert that particular field to database only the id/key is stored rather than the actual value.但是当我将该特定字段插入数据库时,只存储 id/key 而不是实际值。 Any guess how to request for the value rather than key/id in controller.任何猜测如何在 controller 中请求值而不是键/ID。

Here is the controller function:这是 controller function:

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

Here is html code:这是 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> 

Here is the script:这是脚本:

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

Here is the requested data and storing to database:这是请求的数据并存储到数据库:

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

Modify Your insertInv function修改您的 insertInv function

Remove input() from $price = $request->input('price');$price = $request->input('price');中删除 input() OR dd($request->all());dd($request->all()); and check all the values in controller are in correct format And Post the Result here并检查 controller 中的所有值格式是否正确并在此处发布结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让AngularJS路由器在导航时更新控制器值,而不是重新加载整个控制器? - How to have AngularJS router update a controller value upon navigation, rather than reloading the entire controller? 如何使用Javascript,使用类名而不是Id来将值分配给TextArea两者? - How to assign value to TextArea using Javascript, using class name rather than Id to link the two? 如何通过使用javascript获取yii2中的列值而不是ID - How to get column value rather than ID in yii2 by using javascript Javascript:按数字索引而不是键值循环对象 - Javascript: Loop Through Object By Number Index Rather Than Key-Value Axios post 方法正在发送一个对象,其中我的数据作为键和空值,而不是整个数据对象本身 - Axios post method is sending a object with my data as the key and empty value rather than the whole data object itself 在将值id更改为数组键之后,如何在香料之后用数组键替换值id jquery - how to replace value id jquery with array key, after spice than change value id to array key Jquery Autocomplete 键按下显示标签名称而不是值? - Jquery Autocomplete key press down display label name rather than value? Localhost返回“未定义”而不是唯一ID - Localhost returning “Undefined” rather than the unique ID SailsJS策略请求方法而不是路由 - SailsJS policy on request method rather than route 我可以在ID是键而不是ASP.NET MVC(C#)中的数组的情况下输出JSON吗 - Can I output JSON where the ID is the key rather than it being an array in ASP.NET MVC (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM