Back

How do i get array elements displayed in a laravel email view? URGENT!


am trying to send an email via laravel but the array values am sending are not being echoed out on the view.

am fetching rows from the database as an array and saving it as $data. the date values are echoed out but the db array values are not being displayed despite being avalilable when I var_dump($data).

How do i get the data to display in the email view in laravel 4.0? Please help.

the controller MailAlert.php

public function MailAlerts($email)
{
    //get all expired entries that match a particular date selecting only amount and due date columns
        $alerts = Alert::where('on', '=', '2014-10-13')->where('expired', '=', 1)->get(array('amount','due_date'));
    //add alerts to the data array among other variables to be passed to the view
        $data = array('alerts' => $alerts, 'date' => '2014-10-13');
        Mail::queue('emails.alerts', $data, function($message) use($email)
        {
            $message->to($email)->subject('Expiry notice');
        });
}

the email view alerts.blade.php

<html lang="en-US">
<head>
    <meta charset="utf-8">
</head>
<body>
    <div>
        <h2>Alerts</h2>
        <ul class="pin-ul">
            @if(count($alerts))
                Hi your accounts expire on
                @foreach ($alerts as $key)
                    {{ $key->due_date }} outstanding balance is {{ $key->amount }}
                @endforeach
            @else
                <p>No alerts</p> 
            @endif 
            sent on {{ $date }}
        </ul>
    </div>
</body>
</html>

kindly assist. can't seem to make it work

urgent.

jimgwhit replied 4 years ago Solution

Have you tried passing $alerts seperately with a ->with.

(other stuff being passed)->with('data2',$alerts);
dtmuturi replied 4 years ago

hi jimgwhit, thanks for the reply.. yes i tried that too it works. the problem is that when you send the email alerts the data in the array is not there. passing it's elements as a variable they are visible but as an array it just doesn't work. the foreach loop in the email template just echoes a blank space!

jimgwhit replied 4 years ago

Try iterating through array first and put the data in a string format like:

$mydata1 | $mydata2 | $mydata3

using pipe as seperator: Then use the explode function in php to place the data.
http://us.php.net/manual/en/function.explode.php
Another is split, but it's deprecated, but will still be around a while.

dtmuturi replied 4 years ago

jimgwhit said:

Have you tried passing $alerts seperately with a ->with.

(other stuff being passed)->with('data2',$alerts);
~~~>**jimgwhit** said:

Try iterating through  array first and put the data in a string format like:  

$mydata1 | $mydata2 | $mydata3

using pipe as seperator:
Then use the explode function in php to place the data.  
http://us.php.net/manual/en/function.explode.php  
Another is split, but it's deprecated, but will still be around a while.
dtmuturi replied 4 years ago

that works neat trick imploding and exploding the array...

thanks much

dtmuturi replied 4 years ago

dtmuturi said:

that works neat trick imploding and exploding the array...

thanks much>dtmuturi said:

this should be the answer marked the wrong one

dtmuturi replied 4 years ago

also found out changing the array element keys from objects to old school boxy braces works like so

<?php
					if(count(($alerts))
					{
						 Hi your accounts expire on
						 foreach(($alerts as $key)
						{
							echo '<li> #' .  $key['due_date'] . 'outstanding balance is '. $key['amount'] . ' </li>';				
						}
					}
					else
					{
						echo 'no alerts';
						
					}
				?>

instead of

@if(count($alerts))
                Hi your accounts expire on
                @foreach ($alerts as $key)
                    {{ $key->due_date }} outstanding balance is {{ $key->amount }}
                @endforeach
            @else
                <p>No alerts</p> 
            @endif
dtmuturi replied 4 years ago

thanks much jimgwhit. coffee on me. :-)


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us