Argument 1 passed to App\Mail\SurveyMail::__construct() must be an instance of App\Mail\User, array give

Viewed 9995

I'm trying to send emails to the users who have been accepted by the admin using mailable api in Laravel 5.3.

class SurveyMail extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mail.send')
        ->from('monsite@chezmoi.com');
    }

and this is my controller

 class EmailController extends Controller
{
    public function send(Request $request,User $user)
    {    
         Mail::to($user)
         ->send(new SurveyMail ($request->except('_token')));
    }
}

the view:

<body style="background: black; color: white">
 <h2>Prise de contact sur mon beau site</h2>
    <p>Réception d'une prise de contact avec les éléments suivants :</p>
    <ul>

      <li><strong>Nom</strong> : {{ $user->name }}</li>
      <li><strong>Email</strong> : {{ $user->email }}</li>


</body>

it seems that the argument User which is passed to the constructor is not accepted. Please, how can I fix this?

3 Answers

You can do following which worked for me.

From your SurveyMail constructor, remove 'User' before $user and make the constructor look like following

public function __construct($user)
{
    $this->user = $user;
}

This should fix the error.

Related