【问题标题】:Undefined variable: jum (View: C:\laragon\www\mobilenew\resources\views\home.blade.php)未定义变量:jum(查看:C:\laragon\www\mobilenew\resources\views\home.blade.php)
【发布时间】:2021-01-08 07:43:10
【问题描述】:

此时我想用 Laravel 显示每月的财务数据。但我有一个错误。

这是我的控制器

$data = pesanan::where('status_pesanan','5')->whereDate('created_at',date('m'))->get();

status_pesanan 是完成 5 个订单时的订单状态

以下是视图中的代码

@foreach($rute as $rt)
  <?php $total = array(); ?>
  @foreach($data->where('kode_rute1',$rt->kode_rute) as $caritotal)
  <?php
    $total[] = $caritotal->total_bayar;
    $jum = array_sum($total);
    ?>
  @endforeach
@endforeach

total_bayar 是订单的收入

$rute 是 store 分支的循环表

请帮我解决这个问题。

【问题讨论】:

  • 请显示错误信息
  • 是的,我的标题

标签: laravel laravel-8


【解决方案1】:

您尚未全局声明变量 $jum 并将其分配为数组。这将始终返回错误,因为它尚未设置。

所以,尝试全局声明变量。

<?php
    $jum=null;
?>

@foreach($rute as $rt)
  <?php $total = array(); ?>
  @foreach($data->where('kode_rute1',$rt->kode_rute) as $caritotal)
  <?php
    $total[] = $caritotal->total_bayar;
    $jum = array_sum($total);
    ?>
  @endforeach
@endforeach

编辑:你为什么在两个循环中使用 $jum?这意味着每次循环 $jum 都会被修改。

【讨论】:

  • 这确实解决了错误,但我想显示我在 $rute 变量中循环的每个分支中的销售总数据。
  • 此问题与您的主要问题无关
  • 哦是的对不起,因为有人问出了什么错误,所以我把它写在了问题的标题中。
猜你喜欢
  • 1970-01-01
  • 2019-11-07
  • 2019-11-07
  • 2021-02-19
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多