数据数组不被传递到邮件视图

时间:2019-08-21 07:02:56

标签: laravel

我正在尝试将包含多个用户数据的多维数组传递到我的视图,以便可以将其作为列表发送到电子邮件中。但是当我在邮件视图中调用数组时,总是收到错误“未定义的变量”。

我已经尝试过使用foreach调用数组,因为它们都没有起作用,也尝试直接在视图上调用数组中的键,但是我也得到了“未定义的变量”。

功能

    public function handle()
    {
        $tubes = DB::table('tubes')->where('status','received')->select('TubeID', 'TestArrivalDate')->get();
        $tubes = json_decode($tubes, true);
        if(!is_null($tubes)){
        $date = date('Y-m-d H:i:s');
          foreach ($tubes as $tube) {
            $diff = Carbon\Carbon::parse($tube['TestArrivalDate'])->diffInDays();
            $Email = DB::table('tubes')->join('profile', 'profile.ProfileID', 'tubes.ProfileID')->where('TubeID', $tube['TubeID'])->select('ProfileEmail', 'ProfileName')->get();
            $Email = array_flatten(json_decode($Email, true));
            if ($diff > 14) {
              $TubesDelay[]=array('TubeID' => $tube['TubeID'],'TestArrivalDate' => $tube['TestArrivalDate'], 'DaysWaiting' => $diff, 'ProfileEmail' => $Email[0], 'ProfileName' => $Email[1]);
            }
          }
          // dd($TubesDelay);
          Mail::send('mail.DelayedSamplesMail', $TubesDelay, function($message)
          {
              $message->from('kundenservice@test', 'kundenservice@test');
              $message->to(['m.lara@test'])->subject('Sample Delay');
          });
        }
   }

查看:

  <span style="font-size: 11pt; font-family: Poppins Light">
    <br />
    Following Samples are received and have delay:
    @foreach ($TubesDelay as $sample)
      <td>{{$sample}}</td>
    @endforeach
    <br />
    <br />
    <br />
  </span>

我想要的是将所有用户数据放入列表中的电子邮件列表中。所以我的数组看起来像这样

array:5 [
  0 => array:5 [
    "TubeID" => 1005029
    "TestArrivalDate" => "2019-06-21"
    "DaysWaiting" => 61
    "ProfileEmail" => "mat@test.world"
    "ProfileName" => "MLB"
  ]
  1 => array:5 [
    "TubeID" => 1005034
    "TestArrivalDate" => "2019-07-08"
    "DaysWaiting" => 44
    "ProfileEmail" => "mat@test.com"
    "ProfileName" => "MLB2"
  ]

我希望列出数组中的所有数据

TubeID 1005029
TestArrivalDate 2019-06-21
DaysWaiting 61
ProfileEmail mat@test.world
ProfileName MLB

TubeID 1005034
TestArrivalDate2019-07-08
DaysWaiting 44
ProfileEmail mat@test.com
ProfileName MLB2

2 个答案:

答案 0 :(得分:1)

您应该为发送到视图的数组设置键。

Mail::send('mail.DelayedSamplesMail', ['TubesDelay' => $TubesDelay], function($message) {...

该键稍后将用作变量。因此,您可以在视图中循环$TubesDelay

编辑 更改循环以显示所有循环

<span style="font-size: 11pt; font-family: Poppins Light">
    <br />
    Following Samples are received and have delay:
    @foreach ($TubesDelay as $item)
        <td>{{ $sample['TubeID'] }}</td>
        <td>{{ $sample['TestArrivalDate'] }}</td>
        <td>{{ $sample['DaysWaiting'] }}</td>
        <td>{{ $sample['ProfileEmail'] }}</td>
        <td>{{ $sample['ProfileName'] }}</td>
    @endforeach
    <br />
    <br />
    <br />
</span>

答案 1 :(得分:1)

如果您可以dd($ TubesDelay),则尝试使用以下代码:

Mail::send('mail.DelayedSamplesMail', $TubesDelay, function($message) use($TubesDelay)
{
  $message->from('kundenservice@test', 'kundenservice@test');
  $message->to(['m.lara@test'])->subject('Sample Delay');
});