未定义的变量:jum(视图:C:\ laragon \ www \ mobilenew \ resources \ views \ home.blade.php)

时间:2020-09-22 07:54:44

标签: laravel laravel-8

我现在想用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是商店分支的循环表

请帮助我解决此问题。

1 个答案:

答案 0 :(得分:0)

您尚未全局声明变量$ 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?这意味着eveytime循环$ jum将被修改。