title: Laravel - production.ERROR: Undefined variable: data
body: <p>In my Laravel-5.8, I have this code:</p>
  <pre><code>public $filters = [
      &quot;all&quot; =&gt; &quot;all&quot;,
      &quot;logged_in&quot; =&gt; &quot;logged in users&quot;,
      &quot;not_logged_in&quot; =&gt; &quot;not logged in users&quot;,
  ];
  
  public function user_logs($id = &quot;&quot;)
  {
      $userCompany = Auth::user()-&gt;company_id;
      $userEmployee = Auth::user()-&gt;employee_id;
      $userId = Auth::user()-&gt;id;
      $employeecode = Auth::user()-&gt;employee_code;
      
      
      if ($id == 'all') {
          $data = User::where('hr_status', 0)-&gt;where('company_id', $userCompany)-&gt;orderBy('last_login_at', 'desc')-&gt;get();
      } 
      elseif ($id == &quot;&quot;) {
          $data = User::where('hr_status', 0)-&gt;where('company_id', $userCompany)-&gt;where('active', 0)-&gt;orderBy('last_login_at', 'desc')-&gt;get();
      } 
      elseif ($id == &quot;logged_in&quot;) {
          $data = User::where('last_login_at', '&gt;', today()-&gt;subDays(30))
              -&gt;where('hr_status', 0)
                  -&gt;where('company_id', $userCompany)
                  -&gt;orderBy('last_login_at', 'desc')
                  -&gt;get();
      }        
      elseif ($id == &quot;not_logged_in&quot;) {
          $data = User::where('last_login_at', '&lt;', today()-&gt;subDays(30))
                  -&gt;orWhereNull('last_login_at')
              -&gt;where('hr_status', 0)
                  -&gt;where('company_id', $userCompany)
                  -&gt;orderBy('last_login_at', 'desc')
                  -&gt;get();
      }  
  
      $chart_settings = [
          'chart_title'        =&gt; 'Users By Months',
          'chart_type'         =&gt; 'line',
          'report_type'        =&gt; 'group_by_date',
          'model'              =&gt; 'App\\User',
          'group_by_field'     =&gt; 'last_login_at',
          'group_by_period'    =&gt; 'month',
          'aggregate_function' =&gt; 'count',
          'filter_field'       =&gt; 'last_login_at',
          'column_class'       =&gt; 'col-md-12',
          'entries_number'     =&gt; '5',
      ];
      $chart = new LaravelChart($chart_settings);      
  
      return view('report.report_user_login_logs.user_logs', compact( 'chart'))
              -&gt;with('employees', $data)
              -&gt;with('filters', $this-&gt;filters)
              -&gt;with('selectedFilter', $id);
  }
  </code></pre>
  <p>view blade:</p>
  <p>view/report/report_user_login_logs/index.blade.php</p>
  <pre><code>            &lt;div class=&quot;form-group&quot;&gt;
              &lt;select class=&quot;form-control&quot; id=&quot;filter&quot;&gt;
                  &lt;option value=&quot;select&quot;&gt;Select Search Criteria&lt;/option&gt;
                  @foreach($filters as $filter)
                  &lt;option value=&quot;{{$filter}}&quot; @if($filter==$selectedFilter) selected @endif&gt;{{ucfirst(trans($filter))}}&lt;/option&gt;
                  @endforeach
              &lt;/select&gt;
              &lt;/div&gt;
              &lt;tbody&gt;
                  @foreach($employees as $key =&gt; $employee)
                  &lt;tr&gt;
                      &lt;td&gt;
                          {{$employee-&gt;employee_code}}
                      &lt;/td&gt;  
                      &lt;td&gt;
                          {{$employee-&gt;first_name}} {{$employee-&gt;last_name}}
                      &lt;/td&gt;  
                      &lt;td&gt;
                          {{$employee-&gt;email}}
                      &lt;/td&gt;    
                      &lt;td&gt;
                          {{$employee-&gt;last_login_at}}
                      &lt;/td&gt;                         
                  &lt;/tr&gt;
                  @endforeach
              &lt;/tbody&gt;
  
  
  &lt;script type=&quot;text/javascript&quot;&gt;
      $(document).ready(function () {
          $(&quot;#filter&quot;).change(function(e){
              if ($(this).val()=== &quot;select&quot; ){
  
                  var url = &quot;{{route('report.report_user_login_logs.user_logs')}}/&quot;
              }
              else{
                  var url = &quot;{{route('report.report_user_login_logs.user_logs')}}/&quot; + $(this).val();
              }
  
              if (url) {
                  window.location = url;
              }
              return false;
          });
      });
  &lt;/script&gt;
  </code></pre>
  <p>route/web.php</p>
  <pre><code>   Route::group(['prefix' =&gt; 'report', 'as' =&gt; 'report.', 'namespace' =&gt; 'Report', 'middleware' =&gt; ['auth']], function () {
  
      Route::get('report_user_login_logs/user_logs/{id?}', 'ReportUserLoginLogsController@user_logs')-&gt;name('report_user_login_logs.user_logs');
  
     });
  </code></pre>
  <p>I use the dropdown as filter to display the data on the table. The doropdown onchange:</p>
  <pre><code>            &lt;select class=&quot;form-control&quot; id=&quot;filter&quot;&gt;
                  &lt;option value=&quot;select&quot;&gt;Select Search Criteria&lt;/option&gt;
                  @foreach($filters as $filter)
                  &lt;option value=&quot;{{$filter}}&quot; @if($filter==$selectedFilter) selected @endif&gt;{{ucfirst(trans($filter))}}&lt;/option&gt;
                  @endforeach
              &lt;/select&gt;
  </code></pre>
  <p>When I select &quot;All&quot;, it works.</p>
  <p>But when I select</p>
  <pre><code>     &quot;logged in users&quot;   as in  &quot;logged_in&quot; =&gt; &quot;logged in users&quot;,
  </code></pre>
  <p>or</p>
  <pre><code> &quot;not logged in users&quot;    &quot;not_logged_in&quot; =&gt; &quot;not logged in users&quot;,
  </code></pre>
  <p>I got this error:</p>
  <blockquote>
  <p>production.ERROR: Undefined variable: data</p>
  </blockquote>
  <p>How do I resolve it?</p>
  <p>Thank you</p>