Pārlūkot izejas kodu

专题列表返回数据微调

duqinya 6 gadi atpakaļ
vecāks
revīzija
19998f9c26

+ 1 - 8
app/Http/Controllers/CmsSubjectController.php

@@ -31,14 +31,7 @@ class CmsSubjectController extends BaseController
         $cmsSubject = $this->cmsSubjectRepository->index($request->all());
         if (count($cmsSubject)>0) {
             foreach ($cmsSubject as $k => $v) {
-                $v->product_count = 0;
-                $product = CmsSubjectProduct::where('subject_id',$v['id'])->get();
-                $count = $product->toArray();
-                foreach ($count as $key => $value){
-                    if ($value['subject_id'] == $v['id']){
-                        $v->product_count ++ ;
-                    }
-                }
+                $v->product_count = CmsSubjectProduct::where('subject_id', $v['id'])->count();
             }
         }
 

+ 13 - 8
app/Repositories/CmsSubjectRepository.php

@@ -69,17 +69,19 @@ class CmsSubjectRepository {
                     throw new HttpException(500, '请检查商品与排序数目是否对应');
                 }
 
-                $subject_product = new CmsSubjectProduct();
+                $resert_data = [];
                 for ($i = 0;$i < $product_num;$i++){
-                    $resert_data =[
+                    $resert_data[] =[
                         'product_id' => $products[$i],
                         'sort' => $sort[$i],
                         'subject_id' => $res['id'],
                         'created_at' => $date,
                         'updated_at' => $date,
                     ];
-                 $subject_product->insert($resert_data);
-
+                }
+                $result = $this->cmsSubjectProduct->insert($resert_data);
+                if (!$result){
+                    throw new HttpException(500, '专题商品添加失败');
                 }
             }
             DB::commit();
@@ -124,16 +126,19 @@ class CmsSubjectRepository {
                     throw new HttpException(500, '请检查商品与排序数目是否对应');
                 }
 
-                $subject_product = new CmsSubjectProduct();
-                for ($i = 0; $i < $product_num; $i++) {
-                    $resert_data = [
+                $update_data = [];
+                for ($i = 0;$i < $product_num;$i++){
+                    $update_data[] =[
                         'product_id' => $products[$i],
                         'sort' => $sort[$i],
                         'subject_id' => $subject->id,
                         'created_at' => $date,
                         'updated_at' => $date,
                     ];
-                    $subject_product->insert($resert_data);
+                }
+                $result = $this->cmsSubjectProduct->insert($update_data);
+                if (!$result){
+                    throw new HttpException(500, '专题商品添加失败');
                 }
             }
             DB::commit();