Browse Source

分类添加,修改

Moell 9 years ago
parent
commit
5ae69e7ef3

+ 11 - 2
app/Http/Controllers/Backend/CategoryController.php

@@ -56,7 +56,11 @@ class CategoryController extends Controller
      */
     public function store(CreateRequest $request)
     {
-        dd($request);
+        $result = $this->category->store($request->all());
+        if ($result) {
+            return redirect('backend/category')->with('success', '分类添加成功');
+        }
+        return redirect(route('backend.category.create'))->withErrors('系统异常,分类添加失败');
     }
 
     /**
@@ -91,7 +95,12 @@ class CategoryController extends Controller
      */
     public function update(UpdateRequest $request, $id)
     {
-        //
+
+        $result = $this->category->update($request->all(), $id);
+        if ($result) {
+            return redirect('backend/category')->with('success', '分类修改成功');
+        }
+        return redirect(route('backend.category.edit', ['id' => $id]))->withErrors('系统异常,分类修改失败');
     }
 
     /**

+ 59 - 0
app/Repositories/CategoryRepositoryEloquent.php

@@ -32,6 +32,65 @@ class CategoryRepositoryEloquent extends BaseRepository implements CategoryRepos
         return $this->model->getNestedList('name', null, '    ');
     }
 
+    /**
+     * @param $input
+     * @return bool
+     */
+    public function store($input)
+    {
+        if ($input['cate_id'] == 0) {
+            return $this->model->create(['name' => $input['name']]) ? true : false;
+        }
+
+        $category = $this->model->find($input['cate_id']);
+        if (!$category) {
+            return false;
+        }
+
+        return $category->children()->create(['name' => $input['name']]) ? true : false;
+    }
+
+    /**
+     * 分类修改
+     *
+     * @param array $attributes
+     * @param $id
+     * @return bool
+     */
+    public function update(array $attributes, $id)
+    {
+        $input['name'] = $attributes['name'];
+        $parentId = $attributes['cate_id'];
+
+        $category = $this->model->find($id);
+        if (!$category) {
+            return false;
+        }
+
+        if (!parent::update($input, $id)) {
+            return false;
+        }
+
+        if ($parentId != 0 && $category->parent_id != $parentId) {
+
+            $parentCategory = $this->model->find($parentId);
+            if (!$parentCategory) {
+                return false;
+            }
+
+            if (!$category->makeChildOf($parentCategory)) {
+                return false;
+            }
+        } elseif ($category->parent_id != $parentId && $parentId == 0) {
+            //顶级分类
+            if (!$category->makeRoot()) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
     /**
      * Boot up the repository, pushing criteria
      */

+ 8 - 0
resources/views/backend/alert/success.blade.php

@@ -0,0 +1,8 @@
+@if(session('success'))
+    <div class="alert alert-success alert-dismissible">
+        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
+        <i class="icon fa fa-check"></i>
+        提示消息!
+        <p>{{ session('success') }}</p>
+    </div>
+@endif

+ 1 - 0
resources/views/backend/category/index.blade.php

@@ -11,6 +11,7 @@
 @section('content')
     <div class="row">
         <div class="col-xs-12">
+            @include('backend.alert.success')
             <div class="box box-solid">
                 <!-- /.box-header -->
                 <div class="box-header">