title: Couldn't get relation model value in maatwebsite laravel
body: <p>The work of this function is to generate report of specific condition. Thus i am generating report with two tables ( User and Booking ) With primary key is userid and bookingid. Both the table is be clubbed into relations. Now i want to generate excel with using maatwebsite package at this condition. From (booking table) and to   (booking table) with ticketstatus (booking table) and also with usertype ( from usertable ). For example From 01.09.2018 to 23.09.2018 with ticket status as "booked " and usertype as "Normal or agent". But i am getting an error, i am using FromQuery method in maatwebsite to perform this function.</p>
  
  <p>I am adding all the codes here, User Model :</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  use App\Booking;
  use App\Wallet;
  
  use Illuminate\Database\Eloquent\Model;
  
  class User extends Model
  {
      protected $primaryKey = 'userid';
  
      protected $fillable = ['name', 'phone', 'email','password','usertype'];
  
      protected $dates = [
          'createdAt'
      ];
  
      const CREATED_AT = 'createdAt';
      const UPDATED_AT = 'updatedAt';
  
      public function bookings()
      {
          return $this-&gt;hasMany('App\Booking', 'userid');
      }
  
      public function walletUsers()
      {
          return $this-&gt;hasOne('App\Wallet', 'userid');
      }
  
      public function supports()
      {
          return $this-&gt;hasMany('App\Help', 'userid');
      }
  
      public function getNameAttribute($value)
      {
          return ucfirst($value);
      }
  }
  </code></pre>
  
  <p>Booking Model :</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  use Carbon\Carbon;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Booking extends Model
  {
      protected $primaryKey = 'bookingid';
  
      protected $dates = [
          'createdAt','updatedAt'
      ];
  
      const CREATED_AT = 'createdAt';
      const UPDATED_AT = 'updatedAt';
  
      public function users()
      {
          return $this-&gt;belongsTo('App\User', 'userid');
      }
  
      public function getDateOfIssueAttribute($value) {
  
          return Carbon::parse($value)-&gt;format('d-M-Y , h:m a');
      }
  
      public function getDateOfCancellationAttribute($value) {
  
          return Carbon::parse($value)-&gt;format('d-M-Y , h:m a');
      }
  
      public function getDojAttribute($value) {
  
          return Carbon::parse($value)-&gt;format('d-M-Y , h:m a');
      }
  }
  </code></pre>
  
  <p>Now Controller : </p>
  
  <pre><code>public function report(Request $request){
      $from = $request-&gt;from;
      $to = $request-&gt;to;
      $bookingtype = $request-&gt;bookingtype;
      $usertype = $request-&gt;usertype;
  
      return (new BookingsExport($from, $to, $bookingtype, $usertype))-&gt;download('invoices.xlsx');
  }
  </code></pre>
  
  <p>Route :</p>
  
  <pre><code>Route::post('/admin/reports/collect',[
      'uses' =&gt; 'ReportController@report',
      'as' =&gt; 'reports.generate'
  ]);
  </code></pre>
  
  <p>Maatwebsite Class:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Exports;
  
  use App\Booking;
  use App\User;
  use Carbon\Carbon;
  use App\Http\Controllers\ReportController;
  use Maatwebsite\Excel\Concerns\FromQuery;
  use Maatwebsite\Excel\Concerns\Exportable;
  use Maatwebsite\Excel\Concerns\WithHeadings;
  use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
  use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  use PhpOffice\PhpSpreadsheet\Shared\Date;
  use Maatwebsite\Excel\Concerns\WithColumnFormatting;
  use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  use Maatwebsite\Excel\Concerns\WithMapping;
  
  
  class BookingsExport implements FromQuery, WithStrictNullComparison, WithHeadings, ShouldAutoSize, WithColumnFormatting, WithMapping {
      use Exportable;
  
      public function __construct($from, $to, $bookingtype, $usertype)
      {
          $this-&gt;from = $from;
          $this-&gt;to = $to;
          $this-&gt;bookingtype = $bookingtype;
          $this-&gt;usertype = $usertype;
      }
  
      public function headings(): array
      {
          return [
  
              'Booking Id',
              'Block Key',
              'Bus Type',
              'DOJ',
              'status',
              'Created At',
              'Updated At',
              'Usertype',
              'Name'
          ];
      }
  
      public function map($booking): array
      {
          return [
              $booking-&gt;bookingid,
              $booking-&gt;blockkey,
              $booking-&gt;busType,
              $booking-&gt;doj,
              $booking-&gt;status,
              $booking-&gt;createdAt,
              $booking-&gt;updatedAt,
              $booking-&gt;users-&gt;usertype,
              $booking-&gt;users-&gt;name
          ];
      }
  
  
      public function columnFormats(): array
      {
          return [
              'D' =&gt; 'dd-mm-yyy',
              'E' =&gt; NumberFormat::FORMAT_DATE_DDMMYYYY
          ];
      }
  
  
      public function query()
      {
          $from = $this-&gt;from;
          $to = $this-&gt;to;
          $bookingtype = $this-&gt;bookingtype;
          $usertype = $this-&gt;usertype;
  
          if(isset($from) &amp;&amp; isset($to) &amp;&amp; is_null($bookingtype) &amp;&amp; is_null($usertype))
          {
              return Booking::query()-&gt;whereBetween('createdAt',[$from, $to]);   
          }
  
  
          if(isset($from) &amp;&amp; isset($to) &amp;&amp; isset($bookingtype) &amp;&amp; is_null($usertype))
          {
              return Booking::query()-&gt;whereBetween('createdAt',[$from, $to])-&gt;where('status','=', $bookingtype);
          }
  
  
          if(isset($from) &amp;&amp; isset($to) &amp;&amp; isset($bookingtype) &amp;&amp; isset($usertype))
          {
              return Booking::query()-&gt;with('users')-&gt;whereHas("users", function($subQuery){ 
                  $subQuery-&gt;where("usertype", "=", $usertype);})-&gt;whereBetween('createdAt',[$from, $to])-&gt;where('status','=', $bookingtype);
          }
      }
  }
  </code></pre>
  
  <p>The error i am getting is "Undefined variable: usertype" from the last query of Maatwebsite class file. But i am seeding all the values from controller to this, i even dd($usertype) but i am getting the value as agent, but it shows error while using in it query ! Kindly guide</p>