2017-03-20 14 views
1

eコマースアプリで商品の税金を計算しようとしています。表税では、私は次の列がありますLaravel 5.3は、コレクションの選択値を使用できません

id - iso_code - vat. 

特定countryid用バットの情報を取得するために私のクエリ:私は$ taxRate値をしますprint_r場合

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

は私が

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

を取得換算値を取得するには、私は使用しました:

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

と私は、次の情報入手:私の考えは単純計算

priceamount = $productprice * $taxRate 
を取得するために$ taxRate値を使用することです

$taxRates = $taxRate->vat; 

Error: Undefined property: Illuminate\Support\Collection::$vat

を使用して、一方

Cannot use object of type stdClass as array

@assadaの提案を使用してUPDATE ------

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

はエラーを与える:

Cannot use object of type stdClass as array

コレクションに返された値を処理する方法について感謝任意のヘルプを。 brgds。

答えて

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(); 
を試しを
関連する問題