I am trying to send a mail with a attachment. Its based on a var from my controller. But I don't know how to pass the content of the variable from my controller to my mail class. My mail view has no problem with using the vars. I need the var in the build method to pick the right file from my directory I generated in my controller.
This returns the following
Missing argument 1 for App\Mail\Aanvraag::build()
Mail class
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\App;
use App\Http\Controllers\aanvragenController;
class Aanvraag extends Mailable
{
use Queueable, SerializesModels;
public $aanvraag;
public function __construct($aanvraag)
{
$this->aanvraag = $aanvraag;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('mails.aanvraag')->attach(url('/exports/'. $aanvraag->naam .'sheet.xls'));
}
}
Controller
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Aanvragen;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use App\Mail\Aanvraag;
class aanvragenController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index() {
if (Auth::id() == 1) {
$aanvragen = Aanvragen::all();
} else {
$userid = Auth::id();
$aanvragen = Aanvragen::where('user_id', $userid)
->orderBy('id')
->take(10)
->get();
}
return view('aanvragen.index', compact('aanvragen'));
}
public function create() {
return view('aanvragen.create');
}
public function store() {
/* $this->validate(request(), [
'studentnummer' => 'unique:aanvragen'
]);*/
$aanvraag = new Aanvragen;
$aanvraag->naam = request('naam');
$aanvraag->studentnummer = request('studentnummer');
$aanvraag->email = request('email');
$aanvraag->telefoonnummer = request('telefoonnummer');
$aanvraag->stagebedrijf = request('stagebedrijf');
$aanvraag->contactpersoon = request('contactpersoon');
$aanvraag->emailContact = request('emailContact');
$aanvraag->telefoonContact = request('telefoonContact');
$aanvraag->begindatum = request('begindatum');
$aanvraag->einddatum = request('einddatum');
$aanvraag->user_id = Auth::id();
$aanvraag->save();
$data = array (
array('Naam', 'Studentnummer', 'Email', 'Telefoonnummer', 'Stagebedrijf', 'Contactpersoon', 'Email contactpersoon'
, 'Telefoon contactpersoon', 'begindatum', 'einddatum'),
array($aanvraag->naam, $aanvraag->studentnummer, $aanvraag->email, $aanvraag->telefoonnummer, $aanvraag->stagebedrijf,
$aanvraag->contactpersoon, $aanvraag->emailContact, $aanvraag->telefoonContact, $aanvraag->begindatum, $aanvraag->einddatum
)
);
$attachment = $aanvraag->naam;
\Excel::create($aanvraag->naam.'sheet', function ($excel) use ($data) {
$excel->sheet('Sheet1', function ($sheet) use ($data) {
$sheet->fromArray($data);
});
})->save();
\Mail::to('johndoe@info.com')->send(new Aanvraag($aanvraag));
return redirect('/');
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire