title: Import CSV file to Laravel controller and insert data to two tables
body: <p>So I am a complete noob to Laravel and am trying something here. I want to import a CSV file into two tables, I have a table called lists that will get the list name and a <code>client_id</code>.</p>
  
  <p>Then I have a table called customers that will get name surname contact number as well as <code>client_id</code> and a <code>list_id</code>. </p>
  
  <p>What I want to achieve is to import a CSV file that will take the file name and store it in the list table, then create an array through the CSV file and import the data into the customers table with the list and client id's.</p>
  
  <p>I have the first part done, and it inserts into the lists table correctly, How do I now create an array from the CSV that is located in storage/documents and then insert that into the customers table?</p>
  
  <pre><code>namespace App\Http\Controllers;
  
  use Input;
  use DB;
  use Illuminate\Http\Request;
  use App\Http\Requests\ListsRequest;
  use App\Lists;
  use App\Clients;
  use App\Http\Requests;
  use App\Http\Controllers\Controller;
  
  class ListsController extends Controller {
  
      public function index()
      {
          // $list_items = Lists::all();
          $clients = Clients::all();
  
          return view('lists.show', compact('clients'));
      }
  
      public function store(Requests\ListsRequest $request)
      {
          $input = $request-&gt;input();
          Lists::create($input);
  
          if (Input::hasFile('name'))
          {
  
              $file = Input::file('name');
              $name = time() . '-' . $file-&gt;getClientOriginalName();
  
              $path = storage_path('documents');
  
              $file-&gt;move($path, $name);
  
              // All works up to here
              // All I need now is to create an array
              // from the CSV and insert into the customers database
          }
      }
  }
  </code></pre>
  
  <p>I chose to use the answer that I had accepted but I also played with the other answer and got it to work like this.</p>
  
  <pre><code>public function store(Requests\ListsRequest $request)
  {
      $input = $request-&gt;input();
      $client_id = $request-&gt;input('client_id');
  
      if (Input::hasFile('name'))
      {
          $file = Input::file('name');
          $name = time() . '-' . $file-&gt;getClientOriginalName();
          $path = storage_path('documents');
  
          Lists::create(['client_id' =&gt; $client_id, 'name' =&gt; $name]);
  
          $reader = Reader::createFromPath($file-&gt;getRealPath());
          // Create a customer from each row in the CSV file
          $headers = array();
  
          foreach ($reader as $index =&gt; $row)
          {
              if ($index === 0)
              {
                  $headers = $row;
              } else
              {
                  $data = array_combine($headers, $row);
                  Customers::create($data);
              }
          }
  
          $file-&gt;move($path, $name);
  
          return view('clients');
      }
  }
  </code></pre>