Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AydenWH's avatar

Undefined variable in foreach() but variable valid in dd()

I am using Laravel 5.3 to create a email template feature.

It prompts Undefined variable: month (View: D:\Workspace\XAMPP\htdocs\webminder\resources\views\email\view.blade.php) whenever I enter the URL, http://www.webminder.com/send-reminder/ Do take note this is virtual environment

It shows result when I tried dd($month); "December"

I cannot figure out what is the issue behind the scene.

Although it shows error whenever I run the URL. But the email still manage to send out to Mailtrap.

app\Mail\RenewalReminder.php

public function __construct($param, $todayMonth)
{
    $this->param = $param;
    $this->month = $todayMonth;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    $address = '[email protected]';
    $name = 'Wiz Werx - Renewal Reminder';
    $subject = 'Renewal Reminder';

    return $this->view('email.view')
                ->with(['reminders' => $this->param, 'month' => $this->month])
                ->from($address, $name)
                ->replyto($address, $name)
                ->subject($subject);
}

email\view.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Reminder Renewal</title>
</head>
<body>
    <table>
        <tr>
            <td class="bg-letterhead" colspan="3">
                <img src="demo.jpg" class="img-responsive" alt="Letterhead" />
                <h1>Server Expiry List - {{ $month }}</h1>
            </td>
        </tr>
        <tr>
            <th>
                Web Hosting Server
            </th>
            <th>
                Hosting Provider
            </th>
            <th>
                Expiry Date
            </th>
        </tr>
        @foreach ($reminders as $reminder)
        <tr>
            <td>
                {{ $reminder['server_name'] }}
            </td>
            <td>
                {{ $reminder['service_provider'] }}
            </td>
            <td>
                {{ $reminder['expiry_date'] }}
            </td>
        </tr>
        @endforeach
    </table>
</body>
</html>
0 likes
17 replies
SaeedPrez's avatar

If I don't recall wrong, you need to declare those variables as public in your class..

public param;
public month;
geowrgetudor's avatar

->with() method uses session

So you will be able to get them via

{{ session('month') }}

LE: Scratch that. Can you post $this->view() method?

mercuryseries's avatar

@Tyris Can you show me your send-reminder controller handler?

In order to do a little bit of refactoring, try to rename your param attribute reminders and remove the with(['reminders' => $this->param, 'month' => $this->month]) line. Since now your reminders and month attributes are public, they will be available in your view.

By the way, it's replyTo not replyto.

public $reminders;
public $month;

public function __construct($param, $todayMonth)
{
    $this->reminders = $param;
    $this->month = $todayMonth;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    $address = '[email protected]';
    $name = 'Wiz Werx - Renewal Reminder';
    $subject = 'Renewal Reminder';

    return $this->view('email.view')
                ->from($address, $name)
                ->replyTo($address, $name)
                ->subject($subject);
}
AydenWH's avatar

@mercuryseries

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Mail;
use Carbon\Carbon;

use App\Http\Requests;
use App\Models\Domain;
use App\Models\Server;
use App\Models\Website;
use App\Mail\RenewalReminder;

class MailController extends Controller
{
    public static function sendServerReminder() {

        $servers = Server::orderBy('expiry_date')->get();
        $sub_param = array();
        $param = array();

        $todayMonth = Carbon::now()->format('F');

        foreach ($servers as $server) {
            if(Carbon::now()->diffInDays(new Carbon($server->expiry_date)) <= 7) {
                $sub_param = array(
                    "service_provider" => $server->service_provider,
                    "server_name"   => $server->server_name,
                    "expiry_date"   => $server->expiry_date,
                );
                array_push($param, $sub_param);
            }
        }

        Mail::to('my@emailcom')->send(new RenewalReminder($param, $todayMonth));

        return view('email.view');
    }
}

I tried your method but the error still there. :(

Snapey's avatar

Where is the error.... it will tell you module and line?

otepas's avatar

@Tyris are you sending several emails in the same request?

If so, $month could be defined in the first email you send (and therefore it has a value when you dd() it) but it might be undefined in another send.

Try logging the value instead to see when it is undefined.

mercuryseries's avatar
Level 17

@Tyris You need to pass the todayMonth variable to your email.view view like:

return view('email.view', ['month' => $todayMonth]);
2 likes
AydenWH's avatar

@otepas , It quite weird. Using dd able to display the result but whenever you call the variable it will become undefined.

When it sends via email, although the value is undefined but it still display the correct information in the email.

AydenWH's avatar

@mercuryseries

Your method are workable.

But does it means we double passing the value to view?

Since RenewerReminder.php and MailController.php is passing the same value.

mercuryseries's avatar

@Tyris I'm wondering why are you using the same view in your controller and in your mailable?

mercuryseries's avatar

@Tyris What you did is fine but I will rather display a flash message "Reminder sent!" and redirect the user to another page.

AydenWH's avatar

@mercuryseries

Actually this message will be sent via cron job.

So the flash message and redirect is not an issue.

Just that I am curious why got double passing parameter only. Haha.

1 like

Please or to participate in this conversation.