dimanche 27 septembre 2015

laravel Call to a member function welcome() on null

i want to send mail so i made up a mail class so i can pass user data to send him mail. here is my code class Mailer.php

<?php 
use Mail;
abstract class Mailer{
public function sendTo($user, $subject,$view, $data = [])
{
    Mail::send($view, $data, function($message)
    {
        $message->to($user->email, $user->firstname) 
            ->subject($subject)
            ->from('test@test.com', 'Test');
    });
}
}

my class that extend the Mailer class :

class UserMailer extends Mailer{

public function welcome(User $user){
    $view = 'emails.welcome';
    $data = [];
    $subject = 'test subject';
    return $this->sendTo($user, $subject, $view, $data);
}

i want to call this class from any controller so i can pass user data to send mail but i always got this error Call to a member function welcome() on null : maybe i'm doing something wrong :

use App\UserMailer as Mailer ;

class TestController extends Controller
{
protected $mailer;
public function __construct(Mailer $mailer)
{
    $this->mailer;
}
public function testMailer()
{
    $this->mailer->welcome()->sendTo(1);
    return 'done';
}

I want to send this to user id 1. what is wrong with my code ? thx



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire