Php Laravel 5.3无法使用集合中的选择值
我试图在电子商务应用程序中计算产品的含税价格。在tax表中,我有以下列:Php Laravel 5.3无法使用集合中的选择值,php,mysql,laravel-5,eloquent,Php,Mysql,Laravel 5,Eloquent,我试图在电子商务应用程序中计算产品的含税价格。在tax表中,我有以下列: id - iso_code - vat. 获取特定countryid的增值税信息的查询: $taxRate = DB::table('tax') ->select('vat') ->where('iso_code', $productcountryid) ->get(); 如果我打印我获得的$taxRate值 Illuminate\Support
id - iso_code - vat.
获取特定countryid的增值税信息的查询:
$taxRate = DB::table('tax')
->select('vat')
->where('iso_code', $productcountryid)
->get();
如果我打印我获得的$taxRate值
Illuminate\Support\Collection Object ( [items:protected]
=> Array ( [0] => stdClass Object ( [vat] => 1 ) ) )
要检索我使用的增值税价值,请执行以下操作:
$taxRates=$taxRate[0]['tax'];
我获得以下信息:
无法将stdClass类型的对象用作数组
另一方面,使用
$taxRates = $taxRate->vat;
错误:未定义的属性:Illumb\Support\Collection::$vat
我的想法是使用$taxRate值进行简单计算
priceamount = $productprice * $taxRate
使用@assada建议更新------
给出错误:
无法将stdClass类型的对象用作数组
任何关于如何处理集合中返回的值的帮助都值得赞赏。
brgds.
您可以使用以下方法:
$taxRate = DB::table('tax')
->select('vat')
->where('iso_code', $productcountryid)
->get();
$collection = collect($taxRate);
$array = $collection->toArray();
或者试试看
$taxRate = DB::table('tax')
->select('vat')
->where('iso_code', $productcountryid)
->get();
$array = $taxRate->toArray();
$taxRate = DB::table('tax')
->select('vat')
->where('iso_code', $productcountryid)
->get();
$array = $taxRate->toArray();