title: Laravel Undefined Variable: donor ErrorException in d03bd104494df21919e458a02c3243e2b323db06.php line 3:
body: <p>I have a controller which mainly has two functions. First function adds the user to the database and sends an email (receipt.blade.php) to the user.The second function allows the user to get all the receipts for a particular email that a user enters. </p>
  
  <p>Right now, if I directly go to the page where the user can enter the email and get all receipts, its working fine. But, if I try to do it through the process of adding a new user I get the error described in the title after I click submit to adding the user. However, it has added the user to the database, but shows the error because its sending the email which is the receipt.blade.php and has the undefined variable. My controller has these:</p>
  
  <pre><code>use App\Donor;
  use App\Video;
  use Illuminate\Http\Request;
  use DB;
  use Illuminate\Support\Facades\Input;
  use Illuminate\Support\Facades\Mail;
  </code></pre>
  
  <p>The first Function is:</p>
  
  <pre><code>public function thankyoupage(Request $data, Mailer $mailer){
  $donor = Donor::create([
              'first_name'=&gt; $data-&gt;first_name,
              'last_name' =&gt; $data-&gt;last_name,
              'email' =&gt; $data-&gt;email,
              'video_count' =&gt; $video_count,
              'amount_donated' =&gt; $data-&gt;amount_donated,
  });
  
  $mailer
              -&gt;to($data-&gt;input('email'))
              -&gt;send(new \App\Mail\MyMail(($data-&gt;input('first_name')),($data-&gt;input('last_name')),
                  ($data-&gt;input('amount_donated'))));
  
  return redirect()-&gt;action('PagesController@thank1');
      }
  </code></pre>
  
  <p>My mailing file(mailable) is:</p>
  
  <pre><code>class MyMail extends Mailable
  {
      use Queueable, SerializesModels;
  
      public $first_name, $last_name, $amount_donated;
      /**
       * Create a new message instance.
       *
       * @return void
       */
      public function __construct($first_name,$last_name,$amount_donated)
      {
          $this-&gt;first_name = $first_name;
          $this-&gt;last_name = $last_name;
          $this-&gt;amount_donated = $amount_donated;
  
      }
  
      /**
       * Build the message.
       *
       * @return $this
       */
      public function build()
      {
          return $this-&gt;from('myemailid12341@gmail.com')
              -&gt;view('emails.receipt');
      }
  }
  </code></pre>
  
  <p>The second function is:</p>
  
  <pre><code>public function getUserReceipts(Request $data){
      $email = $data-&gt;email;
  
      $donor = Donor::where('email', $email)-&gt;get(); 
  
     return view('emails.receipt')-&gt;with(compact('donor'));
  }
  </code></pre>
  
  <p>The receipt file simply contains:</p>
  
  <pre><code>@foreach($donor as $value)
      {{ $value-&gt;first_name }}
      {{ $value-&gt;last_name }}
      {{ $value-&gt;amount_donated }}
  @endforeach
  </code></pre>
  
  <blockquote>
    <p>The error I'm getting seems to be because donor in the receipt file is
    undefined and I'm not sure how to fix it as it was passed with compact
    in the second function.</p>
  </blockquote>
  
  <p>Would really appreciate the help.</p>