I have a User eloquent model that takes in an instance of the UserMailer class in its constructor but I get this error
Argument 1 passed to User::__construct() must be an instance of TrainerCompare\Mailers\UserMailer, none given, called in /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 631 and defined
I understand the error but can't figure out what I have done wrong but I don't udnerstanding namespacing and composer class map vs psr0 autoloading very well. I have remembered to use composer dump-autoload so it is not that
relevant folder structure
composer.json
app/
models/
User.php
TrainerCompare/
Mailers/
Mailer.php
UserMailer.php
Services/
Validation/
composer.json autoload section. psr-0 section is there from when I added the validation service you can see in TrainerCompare/ and these classes work great. I added app/TrainerCompare/Mailers to the classmap per the tutorial I am following to get the mailer classes loaded
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/tests/helpers",
"app/TrainerCompare/Mailers"
],
"psr-0":{
"TrainerCompare": "app/"
}
}
User.php
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use TrainerCompare\Mailers\UserMailer as Mailer;
class User extends BaseModel implements UserInterface, RemindableInterface
{
protected $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
}
Mailer.php
<?php namespace TrainerCompare\Mailers;
use Mail;
/**
* Email mailing class
*/
abstract class Mailer
{
public function __construct()
{
# code...
}
public function sendTo($user, $subject, $view, $data = [])
{
Maill::send($view, $data, function ($message) use ($user, $subject) {
$message->to($user->email)
->subject($subject);
});
}
}
UserMailer.php
<?php namespace TrainerCompare\Mailers;
use User;
/**
* User Mailer Class
*/
class UserMailer extends Mailer
{
public function __construct()
{
# code...
}
public function welcome($user)
{
$view = 'emails.users.welcome';
$data = [];
$subject = 'Welcome to Laracsts';
return $this->sendTo($user, $subject, $view, $data);
}
}