I know how to pass data to view
but how do you pass data to a mailable
? I'm also a bit confused on where to put the database call DB::select
, in the controller
or the mailable
? So far I've tried this:
web.php
:
Route::get('/test-email', 'App\Http\Controllers\TestMailController@test');
TestMailController.php
:
<?php
namespace App\Http\Controllers;
use App\Mail\ContactMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class TestMailController extends Controller
{
public function test()
{
return new ContactMail();
}
}
TestMail.php
:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
class TestMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $data;
public function __construct()
{
$this->data = DB::select('select * from db_name');
}
public function build()
{
return $this->markdown('emails.testmail')->with('data', $data);
}
}
CodePudding user response:
You need to use $this
:
public function build()
{
return $this->markdown('emails.testmail')->with('data', $this->data);
}