2017-08-04 69 views
0

目前即时通讯使用Laravel-Excel文件(MaatWebsite),并要检查Excel中的数据是否相同与数据库 “家具” 表中的数据Laravel,检查记录是否存在并在表格中显示它们的最佳/备选方法是什么?

控制器

// assume $id is retrieve from parameter 
$check_furnitures = Furniture::where('company_no', $id)->get(); 
$rows = Excel::load('storage\\app\\public\\upload\\furniture.xlsx', function($reader){$reader->noHeading();$reader->skipRows(1)})->get(); 

查看

<table> 
    <thead> 
    <tr> 
     <td>Chair Name></td> 
     <td>Desk Name></td> 
     <td>Carpet Name></td> 
     <td>Status</td> 
    </tr> 
    </thead> 
    <tbody> 
    <?php $arr = array(); ?> 
    @foreach($rows as $key => $value) 
     @foreach($check_furnitures as $fur) 
     @if(
      $fur->chairs == $value[1] && 
      $fur->desks == $value[2] && 
      $fur->carpets == $pvalue[3] 
      ) 
      <?php $temp_array[] = $value[0] ?> 
     @endif 
     @endforeach 
    @endforeach 
    <tr> 
     <td>{{$value[1]}}</td> 
     <td>{{$value[2]}}</td> 
     <td>{{$value[3]}}</td> 
     <td> 
     @foreach($arr as $test) 
      @if($test == $value[0]) 
      DUPLICATED VALUE 
      @endif 
     @endforeach 
     </td> 
    <tr> 
<table> 

Excel文件

enter image description here

+0

人?碰碰撞 – begineeeerrrr

回答

1

最有效的方法是首先为数据库结果建立索引,而不是循环。 Laravel有一个$collection->keyBy('id'),所以你可以在$collection->get($id)以后。

说了这么多,你可以这样来做:

<table> 
    <thead> 
     <tr> 
      <td>Chair Name></td> 
      <td>Desk Name></td> 
      <td>Carpet Name></td> 
      <td>Status</td> 
     </tr> 
    </thead> 
    <tbody> 
     @php 

     $arr = array(); 
     $check_furnitures->keyBy('number'); 

     @endphp 
     @foreach ($rows as $key => $row) 
      <tr> 
       <td>{{ $row->chair }}</td> 
       <td>{{ $row->desk }}</td> 
       <td>{{ $row->table }}</td> 
       <td> 
        @if($check_furnitures->get($row->number) !== null) 
         DUPLICATED VALUE 
        @endif 
       </td> 
      </tr> 
     @endforeach  
    </tbody> 
</table> 
+0

好吧,我明白了,但你知道我如何得到表值的id?因为你可以看到我发布的图片,“Chair-abc”属于表格家具中的“32”,而“Chair-123”属于表格中的“42”,也是如此。你知道如何在视图中获得它的ID吗?我正在考虑做@if($ check_furnitures-> get($ row [1])!== null) – begineeeerrrr

+0

我的建议是,你不能只依赖数字序列来检查它是否存在。最好的做法是在你的excel文件中包含一列ID,这样你就可以检查数据库。 –

+0

注意到谢谢 – begineeeerrrr

相关问题