[英]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.