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

Daniel-Pablo's avatar

How to schedule a foreach inside a email view?

Hello some one can give me a hand with this topic? i got this:

in my Kernel file:

$schedule->call(function () {
            //$plans_n = servHosting::whereMonth('created_at', '=', Carbon::now()->addMonth()->month)->get();
            $plans_n= servHosting::whereMonth('created_at', '=', Carbon::now())->get();
            foreach ($plans_n as $rec) {}

            Mail::to('[email protected]')->send(new HostingReport ($rec));

        })->everyMinute();

in my HostingReport file:


namespace App\Mail;

use App\Models\servHosting;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class HostingReport extends Mailable
{
    use Queueable, SerializesModels;

    public $rec;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(servHosting $rec)
    {
        $this->rec = $rec;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.reports.hosting')
        ->with([
            'plan' => $this->rec->plan,
            'end' => $this->rec->end_date,
            'price' => $this->rec->price,
            'domain' => $this->rec->domain
        ])
        ->subject('Reporte mensual de Hosting - Getweb');
    }
}

in my Hosting email view file:

@component('mail::message')
# Introduction

The body of your message Hosting.

@component('mail::panel')
Resultados proximo mes
@endcomponent

// in this space is going to loop the results
@foreach ($host as $rec)
{{$rec->end}} 
@endforeach

Thanks,<br>
{{ config('app.name') }}
@endcomponent

can anyone help me, please?

0 likes
5 replies
tykus's avatar

What's this?

foreach ($plans_n as $rec) {}

And, what data in the email view template needs to be iterated over? The view template is just Blade, so @foreach works just fine. Combined with the mail::table component, everything is good...

Daniel-Pablo's avatar

Hello, so i can do this {{ end }} on the email view and don't use the foreach?

like this:

@component('mail::table')
| Dominio       | Plan         | Vencimiento  | Renovacion  |
| ------------- |:-------------:| --------:| --------:|
| {{ end }}     | ...      |   ...      | ...      |
@endcomponent
tykus's avatar

What is end - it is not a valid PHP variable; it should be $end (this is what you passed into the view template. Still I don't know what you are iterating over - the $rec variable, or should you be passing $plans_n` into the view?

MichalOravec's avatar
Level 75

In Kernel file:

$plans = servHosting::whereMonth('created_at', '=', Carbon::now())->get();

Mail::to('[email protected]')->send(new HostingReport($plans));

In HostingReport:

public $plans;

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct($plans)
{
    $this->plans = $plans;
}

public function build()
{
    return $this->markdown('emails.reports.hosting')
        ->subject('Reporte mensual de Hosting - Getweb');
}

In view:

@component('mail::table')
| Dominio       | Plan         | Vencimiento  | Renovacion  |
| ------------- |:-------------:| --------:| --------:|
@foreach ($plans as $plan)
| {{ $plan->end }} | ... | ... | ... |
@endforeach
@endcomponent
1 like

Please or to participate in this conversation.