【问题标题】:Laravel 5.3 cannot use select value from collectionLaravel 5.3 不能使用从集合中选择的值
【发布时间】:2017-03-20 13:39:07
【问题描述】:

我正在尝试计算电子商务应用中产品的含税价格。在表格税中,我有以下列:

id - iso_code - vat.

我的查询以获取特定国家/地区的增值税信息:

    $taxRate = DB::table('tax')
        ->select('vat')
        ->where('iso_code', $productcountryid)
        ->get();

如果我 print_r 我获得的 $taxRate 值

Illuminate\Support\Collection Object ( [items:protected] 
=> Array ( [0] => stdClass Object ( [vat] => 1 ) ) ) 

检索我使用的增值税值:

    $taxRates=$taxRate[0]['tax'];

我得到以下信息:

不能使用 stdClass 类型的对象作为数组

另一方面,使用

$taxRates = $taxRate->vat;

错误:未定义属性:Illuminate\Support\Collection::$vat

我的想法是使用 $taxRate 值来获得一个简单的计算

priceamount = $productprice * $taxRate

使用@assada 建议更新 ------

    $array = $taxRate->toArray();
(array)[0]['vat']

给出错误:

不能使用 stdClass 类型的对象作为数组

任何有关如何处理集合中返回的值的帮助表示赞赏。 brgds。

【问题讨论】:

    标签: php mysql laravel-5 eloquent


    【解决方案1】:

    https://laravel.com/docs/5.4/collections

    你可以这样使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      相关资源
      最近更新 更多