Nosean
7 months ago

Passing Data from Mailable Job to Balde view

Posted 7 months ago by Nosean

Find a solution to the following problem: How do I transfer data from the controller (dispatch statement) via the job to the mailable and then to the view?

My Controller:

public function sendjob(Request $request) {

        $date = $request->lieferdatum;
        $today  = Carbon::createFromFormat('d.m.Y', $request->lieferdatum);
        $yesterday = date_format($today->subDay(), 'd.m.Y');


        $i = 5;
        foreach($request->user_id as $kto) {

            $users = DB::table('users')
                ->where('Kto', '=', $kto)
                ->first();

            $details = Array(
                'date' => $date,
                'yesterday' => $yesterday,
                'user_name' => $users->Name,
                'user_name2' => $users->Name2,
                'user_adress' => $users->LieferStrasse,
                'user_zip' => $users->LieferPLZ,
                'user_city' => $users->LieferOrt,
                'user_email' => $users->EMail,
                'user_kto' => $users->Kto
            );

                dispatch(new SendRememberEmailJob($details))->delay($i);

            $i = $i + 5;
        }

    }

My Job:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendRememberEmail;


class SendRememberEmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $details;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $email = new SendRememberEmail($this->details);
        Mail::to($this->details['user_email'])->send($email);
    }
}

And my Mailable:

<?php

namespace App\Mail;

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

class SendRememberEmail extends Mailable
{
    use Queueable, SerializesModels;

    protected $details;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {

        return $this->subject('Bestellerinnerung')
                    ->from('[email protected]')
                    ->view('admin.sendrememberemail')
                    ->with('details', $this->details);


    }
}

ANd My Blade:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Bestellerinnerung</title>
</head>
<body>
Bestellerinnerung

{{ $details->user_email }}

</body>
</html>

Error Message from laravel log file: [2019-02-11 10:08:29] local.ERROR: Undefined variable: details (View: C:\xampp\htdocs\PFPortal\resources\views\admin\sendrememberemail.blade.php) {"exception":"[object] (ErrorException(code: 0): Undefined variable: details (View: C:\xampp\htdocs\PFPortal esources\views\admin\sendrememberemail.blade.php) at C:\xampp\htdocs\PFPortal\storage\framework\views\cabf0836bfe8b9cbd9fb98c097e84b67da3feac3.php:10, ErrorException(code: 0): Undefined variable: details at C:\xampp\htdocs\PFPortal\storage\framework\views\cabf0836bfe8b9cbd9fb98c097e84b67da3feac3.php:10) [stacktrace]

Can anyone Help me ?

Thx

Please sign in or create an account to participate in this conversation.