title: google sheet to laravel
body: <p>I have built a CRM that manage leads </p>
  
  <p>I have a table called leads, and in the controller in <code>index()</code> method <br>, I made a request to google API to get the data from my sheet table.</p>
  
  <p>now what's is happen, that in every page refresh i use <code>updateOrCreate()</code> <br> to update the table 
  and show it in the leads index view with <code>dataTable()</code></p>
  
  <p>my problem is as I mentioned above, that every page refresh it calling the method to google API,<br>
  and it's a performance issue that gets to me in the long run when it is thousand of records</p>
  
  <p>and my question is if there is a way to listen to the sheet table if made changes<br> (edit or added row)
  and updated only in this situation</p>
  
  <p>I have thought about to do another table to the sheet data but I need it updated somehow </p>
  
  <p>I need guidelines for how and if I can to do so,
  or there is a better way to it.</p>
  
  <pre><code>  public function index(Request $request, Lead $lead) 
  {
      $client = new Google_Client();
      putenv('GOOGLE_APPLICATION_CREDENTIALS=../att-sheets-b5343f28dd39.json');
      $client-&gt;useApplicationDefaultCredentials();
      $client-&gt;addScope(Google_Service_Drive::DRIVE);
  
      $driveService = new Google_Service_Drive($client);
  
      // List Files
      // $response = $driveService-&gt;files-&gt;listFiles();
  
      // Set File ID and get the contents of your Google Sheet
      $fileID = '1okqjGbGDnbzJbXDwZ1A6RHJ3LddyL6fGMZWnq88xUiw';
      $response = $driveService-&gt;files-&gt;export($fileID, 'text/csv', array(
            'alt' =&gt; 'media'));
      $content = $response-&gt;getBody()-&gt;getContents();
  
      // Create CSV from String
      $csv = Reader::createFromString($content, 'r');
      $csv-&gt;setHeaderOffset(0);
      $records = $csv-&gt;getRecords();
  
      // Create an Empty Array and Loop through the Records
      $newarray = array();
      foreach ($records as $value) {
          $newarray[] = $value;
      }
  
  
      foreach ($newarray as $data) {
          $matchThese = array('email' =&gt; $data['your-email']);
  
  
  
          Lead::updateOrCreate($matchThese,[
              'user_name'    =&gt; 'admin',
              'name'       =&gt; $data['your-name'],
              'tel'        =&gt; $data['your-tel'],
              'source'     =&gt; 'Google sheets',
              'sheet_date' =&gt; $data['date'].', '.$data['time'],
          ])-&gt;save();
      }
  
  
  
  
      $user = User::all();
  
  
      if ($request-&gt;ajax()) {
          $data = Lead::where('deleted', '!=' , '1')-&gt;orWhereNull('deleted')-&gt;latest()-&gt;get();
          return Datatables::of($data)
                  -&gt;addIndexColumn()
                  -&gt;addColumn('action', function($lead){
                      $btnEdit = '';
                      $btnDel = '';
                      if(auth()-&gt;user()-&gt;can('update', $lead))
                      {
                          $btnEdit = '&lt;a href="/leads/'.$lead-&gt;id.'/edit" class="btn btn-sm btn-primary edit" id="'.$lead-&gt;id.'"&gt;&lt;i class="glyphicon glyphicon-edit"&gt;&lt;/i&gt; Edit&lt;/a&gt;';
                      }
                      if(auth()-&gt;user()-&gt;can('delete', $lead))
                      {
                          $btnDel = '&lt;a href="#" class="btn btn-sm btn-danger delete" id="'.$lead-&gt;id.'"&gt;&lt;i class="glyphicon glyphicon-remove"&gt;&lt;/i&gt; Delete&lt;/a&gt;';
                      }
                      $btnShow = '&lt;a href="/leads/'.$lead-&gt;id.'" class="btn btn-sm btn-success show"&gt;View&lt;/a&gt;';
                      $btn = $btnEdit . $btnDel . $btnShow;
                      return $btn;
                  })
                  -&gt;addColumn('checkbox', function($lead)
                  {
                      $checkbox = '';
                      if(auth()-&gt;user()-&gt;can('delete', $lead))
                      {
                          $checkbox = '&lt;input type="checkbox" name="leads_checkbox[]" class="leads_checkbox" value="'.$lead-&gt;id.'"&gt;';
                          return $checkbox;
                      }
                  })
                  -&gt;rawColumns(['action','checkbox'])
                  -&gt;make(true);
      }
  
      return view('leads.index', compact(
          ['user', 'lead'])
      );
  }
  </code></pre>
  
  <p>thanks for the help!!!</p>