title: Laravel How to attach generated PDF to Email
body: <p>I'm working on a <code>laravel</code>-application where users can fill out and submit a form with some user-data. When the user has submitted the form I receive an email with a confirmation that a user has send me the form with some data.
  Now, in addition to that I have added the feature that a <code>pdf</code> is generated too with the user-data and I want to attach it to the email. So far it works ok, but my issue is that right now I'm getting two emails. One with the confirmation and one with the generated PDF. </p>
  
  <p>This is what my controller looks like:</p>
  
  <pre><code>$application = Applicant::create([
      'email' =&gt; request()-&gt;email,
      'name' =&gt; request()-&gt;name,
      'avatar' =&gt; request()-&gt;avatar,
      'phone' =&gt; request()-&gt;phone,
      'address' =&gt; request()-&gt;address,
      'zipcode' =&gt; request()-&gt;zipcode,
      'city' =&gt; request()-&gt;city,
  ]);
  
  $pdf = PDF::loadView('pdf.application', $application);
  
  Mail::send('pdf.application', $pdf_data, function ($message) use ($pdf_data, $pdf) {
  $message-&gt;to('my@mail.com', $application["name"])
      -&gt;subject('New Applicant - ' . $application["name"])
      -&gt;attachData($pdf-&gt;output(), "application_" . $application["name"] . ".pdf");
  });
  
  Mail::to('my@mail.com')-&gt;send(new NewApplication($application-&gt;fresh()));
  
  return response()-&gt;json('OK', 200););
  </code></pre>
  
  <p>The <code>NewApplication</code>-methods build funtion looks like this:</p>
  
  <pre><code>public function build()
  {
      $build = $this-&gt;replyTo($this-&gt;application-&gt;email, $this-&gt;application-&gt;name)
          -&gt;subject("New Application in Database: {$this-&gt;application-&gt;name}")
          -&gt;view('emails.application')
          -&gt;with([
              'name' =&gt; $this-&gt;application-&gt;name,
              'address' =&gt; $this-&gt;application-&gt;address,
              'email' =&gt; $this-&gt;application-&gt;email,
              'phone' =&gt; $this-&gt;application-&gt;phone
          ]);
  }
  </code></pre>
  
  <p>So, how can I combine these two mail methods so that I only receive one?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>OK, so I tried to do this in my <code>NewApplication.php</code></p>
  
  <pre><code>public function __construct(SingleApplication $application)
  {
      $this-&gt;application = $application;
      $this-&gt;pdf = PDF::loadView('pdf.application', $this-&gt;application);
  }
  
  public function build()
  {
      $build = $this-&gt;replyTo($this-&gt;application-&gt;email, $this-&gt;application-&gt;name)
          -&gt;subject("New application in database: {$this-&gt;application-&gt;name}")
          -&gt;view('emails.application')
          -&gt;attach($this-&gt;pdf, [
              'as' =&gt; 'applicant.pdf', 
              'mime' =&gt; 'application/pdf',
      ]);
  
      return $build;
  }
  </code></pre>
  
  <p>But this fails and gives me the error <code>basename() expects parameter 1 to be string, object given</code> - any other suggestions?</p>