title: Import CSV file in laravel
body: <p>I'm using <code>maatwebsite/excel</code> and I have CSV file including <code>1449</code> rows where I have <code>value</code> and <code>parent title</code> in each row. (here is sample of it)</p>
  
  <p><a href="https://i.stack.imgur.com/4xhMS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4xhMS.png" alt="screen1"></a></p>
  
  <p>what i want is to get <code>id</code> of <code>parents</code> from another table and use those id's instead of titles (which i have in my csv)</p>
  
  <h1>Codes</h1>
  
  <p><code>import function</code></p>
  
  <pre><code>public function importsku(Request $request) {
        Excel::load($request-&gt;file('file'), function($reader) {
          $results = $reader-&gt;all();
  
           foreach($results as $row){
              $findermap = new FinderMap;
              $findermap-&gt;ymm_value_id = $row-&gt;ymm_value_id;
              $findermap-&gt;sku = $row-&gt;sku;
              $findermap-&gt;save();
          }
  
          Session::flash('success', 'Your Data imported successfully.');
          return redirect()-&gt;route('finders.index');
        });
  }
  </code></pre>
  
  <h1>More info</h1>
  
  <p>The parents name that I'm looking to get their <code>id's</code> are stored in table named <strong>finder_ymm_values</strong> where their <strong>dropdown_id</strong> is <code>4</code> <em>I think this will helps us to limit our search and get results faster</em></p>
  
  <h1>Question</h1>
  
  <p>What should I add to my import function in order to search for my parents name and get their id's for each value i have?</p>
  
  <h1>UPDATE</h1>
  
  <p>I added code below to my import function and now it does get the id's that i'm looking for, but the issue is it gets them in collection.</p>
  
  <p><code>code i added</code></p>
  
  <pre><code>foreach($results as $row){
    //added
    $vals = FinderYmmValue::where('title', $row-&gt;ymm_value_id)-&gt;where('dropdown_id', '4')-&gt;pluck('id');
    $findermap = new FinderMap;
    $findermap-&gt;ymm_value_id = $vals; //changed
    $findermap-&gt;sku = $row-&gt;sku;
    $findermap-&gt;save();
  }
  </code></pre>
  
  <p><code>error i get</code></p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
    update a child row: a foreign key constraint fails .......... insert
    into <code>finder_maps</code> (<code>ymm_value_id</code>, <code>sku</code>, <code>updated_at</code>, <code>created_at</code>)
    values ([303], L0R88AA, 2018-06-14 10:41:51, 2018-06-14 10:41:51))</p>
  </blockquote>
  
  <p>As you can see in last line of my error i get my id as <code>[303]</code></p>
  
  <p>any idea?</p>