title: Checking database and avoid duplicate before inserting data by using laravel
body: <p>I am trying to check database before inserting data to avoid duplicate. I store the URLs in array, and inserting those URL in "link" column on a table. So first I am checking the column, istere same URL or not? If not, then insert the data with this code.</p>
  
  <p>Here is my whole codes of database inserting;</p>
  
  <pre><code>foreach ($outlineUrls  as $outlineUrl) {
      $html = file_get_contents($outlineUrl);
      $DOMParser = new \DOMDocument();
      $DOMParser-&gt;loadHTML($html);
  
  
      $changeForMyDB = [
                              'remote_id' =&gt; '',
                              'region' =&gt; '関西',
                              'link' =&gt; json_encode($outlineUrl),
                              'name' =&gt; '',
                              'price' =&gt; '',
                              'extend' =&gt; '',
                              'address' =&gt; '',
                              'hows_old' =&gt; '',
                              'rooms' =&gt; '',
                              'old' =&gt; '',
                              'entery' =&gt; '',
                              'balcon_m2' =&gt; '',
                              'company_name' =&gt; '',
                              'script_from' =&gt; ''
                          ];
  
              $allPTags = $DOMParser-&gt;getElementsByTagName('p');
              foreach($allPTags as $ptag){
                  if($ptag-&gt;getAttribute('class') == 'c-name'){
                          $changeForMyDB['name'] = trim($ptag-&gt;nodeValue);
                  }
              }
  
              $changeForMyDB['address'] = trim($DOMParser-&gt;getElementsByTagName('dd')[0]-&gt;nodeValue);
  
              $allTables = $DOMParser-&gt;getElementsByTagName('table');
              foreach($allTables as $table){
                  foreach($table-&gt;getElementsByTagName('tr') as $tr){
                      $property = trim($tr-&gt;getElementsByTagName('th')[0]-&gt;nodeValue);
                      $value = trim($tr-&gt;getElementsByTagName('td')[0]-&gt;nodeValue);
  
                      switch ($property) {
                          case '物件名':
                              $changeForMyDB['name'] = $value;
                              break;
                          case '販売価格':
                              $changeForMyDB['price'] = $value;
                              break;
                          case '専有面積':
                              $changeForMyDB['extend'] = $value;
                              break;
                          case '所在地':
                              $changeForMyDB['address'] = $value;
                              break;
                          case '総戸数':
                              $changeForMyDB['hows_old'] = $value;
                              break;
                          case '間取り':
                              $changeForMyDB['rooms'] = $value;
                              break;
                          case '竣工時期':
                              $changeForMyDB['old'] = $value;
                              break;
                          case '管理会社':
                              $changeForMyDB['company_name'] = $value;
                              break;
                          case '入居時期':
                              $changeForMyDB['entery'] = $value;
                              break;
                          case '入居時期':
                              $changeForMyDB['entery'] = $value;
                              break;
                          case 'バルコニー面積':
                              $changeForMyDB['balcon_m2'] = $value;
                              break;
                          default:
                              # code...
                              break;
                      }
                  }
              }
  
  if(Estates::where('link','=',$outlineUrl)-&gt;count() &gt; 0)
  {
  
     $this-&gt;error('There is link.');
  
  } else {
  
          Estates::insertGetId($changeForMyDB);
  
          $this-&gt;line('Data saved.');
  
         }
  </code></pre>
  
  <p>But the problem is here. It's inserting the data without checking it!
  Any idea what's the problem is? Thank you, for your help.  </p>