Browse Source

feat: 优化后台foreach 目标为null 的时候 报错

xiansin 2 years ago
parent
commit
24452484b0

+ 24 - 12
server/app/Admin/Controllers/ProductController.php

@@ -39,45 +39,57 @@ class ProductController extends AdminController
             $grid->column('cover_img')->image('',80);
             $grid->column('cover_img')->image('',80);
             $grid->column('cases')->display(function (){
             $grid->column('cases')->display(function (){
                 $html = '';
                 $html = '';
-                foreach ($this->cases as $case){
-                    $html .= '<img data-action="preview-img" src="'.$case.'"
+                if($this->cases){
+                    foreach ($this->cases as $case){
+                        $html .= '<img data-action="preview-img" src="'.$case.'"
 style="max-width:80px;max-height:80px;cursor:pointer"
 style="max-width:80px;max-height:80px;cursor:pointer"
 class="img img-thumbnail">';
 class="img img-thumbnail">';
+                    }
                 }
                 }
                 return $html;
                 return $html;
             })->width(300);
             })->width(300);
             $grid->column('tech_param')->display(function (){
             $grid->column('tech_param')->display(function (){
                 $html = '';
                 $html = '';
-                foreach ($this->tech_param as $key => $item){
-                    $html .= "<a href='{$item['url']}' download target='_blank'>技术参数</a><br>";
+                if($this->tech_param){
+                    foreach ($this->tech_param as $key => $item){
+                        $html .= "<a href='{$item['url']}' download target='_blank'>技术参数</a><br>";
+                    }
                 }
                 }
                 return $html;
                 return $html;
             });
             });
             $grid->column('cad_model')->display(function (){
             $grid->column('cad_model')->display(function (){
                 $html = '';
                 $html = '';
-                foreach ($this->cad_model as $key => $item){
-                    $html .= "<a href='{$item['url']}' download target='_blank'>CAD模型</a><br>";
+                if($this->cad_model){
+                    foreach ($this->cad_model as $key => $item){
+                        $html .= "<a href='{$item['url']}' download target='_blank'>CAD模型</a><br>";
+                    }
                 }
                 }
                 return $html;
                 return $html;
             });
             });
             $grid->column('cad_design')->display(function (){
             $grid->column('cad_design')->display(function (){
                 $html = '';
                 $html = '';
-                foreach ($this->cad_design as $key => $item){
-                    $html .= "<a href='{$item['url']}' download target='_blank'>CAD设计</a><br>";
+                if($this->cad_design){
+                    foreach ($this->cad_design as $key => $item){
+                        $html .= "<a href='{$item['url']}' download target='_blank'>CAD设计</a><br>";
+                    }
                 }
                 }
                 return $html;
                 return $html;
             });
             });
             $grid->column('su_model')->display(function (){
             $grid->column('su_model')->display(function (){
                 $html = '';
                 $html = '';
-                foreach ($this->su_model as $key => $item){
-                    $html .= "<a href='{$item['url']}' download target='_blank'>SU模型</a><br>";
+                if($this->su_model){
+                    foreach ($this->su_model as $key => $item){
+                        $html .= "<a href='{$item['url']}' download target='_blank'>SU模型</a><br>";
+                    }
                 }
                 }
                 return $html;
                 return $html;
             });
             });
             $grid->column('other')->display(function (){
             $grid->column('other')->display(function (){
                 $html = '';
                 $html = '';
-                foreach ($this->other as $key => $item){
-                    $html .= "<a href='{$item['url']}' download target='_blank'>{$item['name']}</a><br>";
+                if($this->other){
+                    foreach ($this->other as $key => $item){
+                        $html .= "<a href='{$item['url']}' download target='_blank'>{$item['name']}</a><br>";
+                    }
                 }
                 }
                 return $html;
                 return $html;
             });
             });

+ 6 - 4
server/app/Admin/Controllers/ProductSpecController.php

@@ -83,11 +83,13 @@ class ProductSpecController extends AdminController
             $grid->column('sort')->editable();
             $grid->column('sort')->editable();
             $grid->column('specs')->display(function (){
             $grid->column('specs')->display(function (){
                 $html = '';
                 $html = '';
-                foreach ($this->specs as $key => $specs){
-                    $br = $key % 3 == 0 && $key != 0 ? '<br>': '';
-                    $html .= "<span class='label' style='background:#586cb1;margin: 0 5px 5px 0;display: inline-block;'> {$specs['name']}(&yen;{$specs['price']})</span>{$br}";
+                if($this->specs){
+                    foreach ($this->specs as $key => $specs){
+                        $br = $key % 3 == 0 && $key != 0 ? '<br>': '';
+                        $html .= "<span class='label' style='background:#586cb1;margin: 0 5px 5px 0;display: inline-block;'> {$specs['name']}(&yen;{$specs['price']})</span>{$br}";
+                    }
+                    return $html;
                 }
                 }
-                return $html;
             });
             });
 
 
             $grid->disableViewButton();
             $grid->disableViewButton();