php - (2/2) ErrorException stdClass 类的对象无法在 laravel 5 中转换为 int

标签 php mysql laravel laravel-5 eloquent

我想创建一个发票的税务摘要,其中我想根据不同的税率添加总计。例如:- 2.5% 税率有自己的产品,6% 税率有自己的产品。我想这样做在 for 循环中。我使用了以下代码,但它不起作用。请告诉我这段代码有什么问题。它没有显示正确的总计。我有一个表名称是 sales_items。字段有id,invoice_id,cgst_percentage,cgst_amount,sgst_percentage等。如图

enter image description here

Controller .php

    $data['query1']= DB::table('sales_items')->distinct()->get(['cgst_percentage']);
    // I am using this variable in foreach loop in view file.

View.blade.php

    <?php $data_total= array();  ?>
    <?php $cgst_unique_data= array(); ?>

 @foreach($query1 as $cgst_data=>$value)


    <?php 

    $cgst_unique_data= DB::table('sales_items')->where('cgst_percentage','=',$value->cgst_percentage)->where('invoice_id','=',$sales_item->invoice_id)->get();

    ?>

     @foreach($cgst_unique_data as $total=>$value) 

     <?php

     $data_total = $total_amount + $total;
     $data_total= $data_total + $value;
     ?> 
     @endforeach

     {{ $data_total }}

 @endforeach

最佳答案

$value 变量是一个对象,用于保存 cgst_unique_data 查询结果中的一行。您不能像在

中那样简单地将其添加到其他整数变量中
$data_total= $data_total + $value;

您需要访问要添加的行中的特定列。如果列名称是 someName,您需要执行以下操作:

$data_total= $data_total + $value->someName;

关于php - (2/2) ErrorException stdClass 类的对象无法在 laravel 5 中转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46986184/

相关文章:

php - mysql-cluster页面无法启动

php - 客户端仅选择时的 SQL 注入(inject)

php - CodeIgniter:如何从 URL 中删除消息?

Mysql查询为每次迭代按顺序对记录进行排序?

php - LARAVEL - 未找到基表或 View : 1146 Table doesn't exist (SQL: select * from )

php - Laravel 调试/堆栈跟踪页面在单击在线时尝试打开 phpstorm 而不是 VSCode

php - 它只发布 "array"的值,而不是预期值

java - 用于验证 SQL 的正则表达式

php - 如何显示从该表单中选择的数据?

php - Laravel Eloquent 的 API 适配器?