Kaynağa Gözat

文章搜索,分类设置为导航

Moell 9 yıl önce
ebeveyn
işleme
ddc54b2ea8

+ 3 - 2
app/Http/Controllers/ArticleController.php

@@ -32,9 +32,10 @@ class ArticleController extends Controller
      *
      * @return \Illuminate\Http\Response
      */
-    public function index(ArticleService $articleService)
+    public function index(ArticleService $articleService, Request $request)
     {
-        $articles = $this->article->orderBy('id', 'desc')->paginate(15);
+        $where = ArticleService::backendSearchWhere($request);
+        $articles = $this->article->backendSearchArticle($where);
         $category   = [];
         $author     = [];
         if ($articles) {

+ 24 - 7
app/Http/Controllers/Backend/CategoryController.php

@@ -2,14 +2,15 @@
 
 namespace App\Http\Controllers\Backend;
 
-use App\Repositories\CategoryRepositoryEloquent;
 use Illuminate\Http\Request;
 
 use App\Http\Requests;
 use App\Http\Controllers\Controller;
-use App\Models\Category;
+use App\Repositories\CategoryRepositoryEloquent;
 use App\Http\Requests\Backend\Category\CreateRequest;
 use App\Http\Requests\Backend\Category\UpdateRequest;
+use App\Repositories\NavigationRepositoryEloquent;
+use Mockery\CountValidator\Exception;
 
 class CategoryController extends Controller
 {
@@ -104,12 +105,28 @@ class CategoryController extends Controller
      */
     public function destroy($id)
     {
-        $category = Category::find($id);
-        if ($category) {
-            if ($category->delete()) {
-                return response()->json(['status' => 0]);
-            }
+        if ($this->category->delete($id)) {
+            return response()->json(['status' => 0]);
         }
         return response()->json(['status' => 1]);
     }
+
+    /**
+     * @param $id
+     * @param NavigationRepositoryEloquent $nav
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function setNavigation($id, NavigationRepositoryEloquent $nav)
+    {
+        try {
+            $category = $this->category->find($id);
+            if ($nav->setCategoryNav($category->id, $category->name)) {
+                return redirect()->back()->with('success', '设置成功');
+            }
+            throw new Exception('设置失败');
+        } catch (\Exception $e) {
+            dd($e->getMessage());
+            return redirect()->back()->withErrors($e->getMessage());
+        }
+    }
 }

+ 1 - 0
app/Http/Controllers/Backend/LinkController.php

@@ -29,6 +29,7 @@ Route::group(['prefix'=>'backend'], function(){
         Route::get('/', 'Backend\HomeController@index');
         Route::resource('article', 'Backend\ArticleController');
         Route::resource('category', 'Backend\CategoryController');
+        Route::get('category/set-nav/{id}', ['as' => 'backend.category.set-nav', 'uses' => 'Backend\CategoryController@setNavigation']);
         Route::resource('user', 'Backend\UserController');
         Route::resource('tag', 'Backend\TagController');
         Route::resource('link', 'Backend\LinkController');

+ 1 - 1
app/Models/Article.php

@@ -10,6 +10,6 @@ class Navigation extends Model implements Transformable
 {
     use TransformableTrait;
 
-    protected $fillable = ['name','url','sequence','state'];
+    protected $fillable = ['name','url','sequence','state', 'article_cate_id', 'nav_type'];
 
 }

+ 15 - 0
app/Models/Tag.php

@@ -45,4 +45,19 @@ class ArticleRepositoryEloquent extends BaseRepository implements ArticleReposit
         $this->applyConditions([['title', 'like', $search]]);
         return $this->paginate(15, ['id','title','desc','user_id','cate_id','read_count','created_at']);
     }
+
+    /**
+     * 搜索文章
+     *
+     * @param array $where
+     * @return mixed
+     */
+    public  function backendSearchArticle(array $where)
+    {
+        if (count($where) > 0) {
+            $this->applyConditions($where);
+        }
+
+        return $this->orderBy('id', 'desc')->paginate(15);
+    }
 }

+ 29 - 1
app/Repositories/ArticleTagRepository.php

@@ -6,7 +6,6 @@ use Prettus\Repository\Eloquent\BaseRepository;
 use Prettus\Repository\Criteria\RequestCriteria;
 use App\Repositories\NavigationRepository;
 use App\Models\Navigation;
-use App\Validators\NavigationValidator;
 
 /**
  * Class NavigationRepositoryEloquent
@@ -33,4 +32,33 @@ class NavigationRepositoryEloquent extends BaseRepository implements NavigationR
     {
         $this->pushCriteria(app(RequestCriteria::class));
     }
+
+    /**
+     * 设置分类为导航
+     *
+     * @param $categoryId
+     * @param $categoryName
+     * @return bool
+     */
+    public function setCategoryNav($categoryId, $categoryName)
+    {
+        $where = [
+            ['article_cate_id', '=', $categoryId],
+            ['nav_type', '=', 1]
+        ];
+        $navigation = $this->findWhere($where);
+        if (!$navigation->isEmpty()) {
+            return true;
+        }
+
+        $create['article_cate_id']  = $categoryId;
+        $create['nav_type']     = 1;
+        $create['name']         = $categoryName;
+        $create['url']          = route('category', ['id' => $categoryId]);
+        if ($this->create($create)) {
+            return true;
+        }
+
+        return false;
+    }
 }

+ 22 - 0
app/Repositories/TagRepository.php

@@ -4,6 +4,7 @@ namespace App\Services;
 
 use App\Repositories\UserRepositoryEloquent;
 use App\Repositories\CategoryRepositoryEloquent;
+use App\Http\Request;
 
 class ArticleService
 {
@@ -73,4 +74,25 @@ class ArticleService
         }
         return $new;
     }
+
+
+    /**
+     * 搜索where条件
+     *
+     * @param $request
+     * @return array
+     */
+    public static function backendSearchWhere($request)
+    {
+        $where = [];
+        if ($request->title != "") {
+            $where[] = ['title', 'like', "%".$request->title."%"];
+        }
+
+        if ($request->cate_id > 0) {
+            $where[] = ['cate_id', '=', $request->cate_id];
+        }
+
+        return $where;
+    }
 }

+ 1 - 5
app/Services/ArticleTagService.php

@@ -14,15 +14,11 @@
             <div class="box box-solid">
                 @include('backend.alert.success')
                 <div class="box-header">
-                    <form class="form-inline">
+                    <form class="form-inline" action="" method="get">
                         <div class="form-group">
                             <label for="title">标题</label>&nbsp;
                             <input name='title' type="text" class="form-control" id="title" placeholder="请输入文章标题">&nbsp;
                         </div>
-                        <div class="form-group">
-                            <label for="tag">标签</label>&nbsp;
-                            <input name='tag' type="text" class="form-control" id="tag" placeholder="请输入文章标签">
-                        </div>
                         <div class="form-group">
                             <label for="cate_id">分类</label>&nbsp;
                             @inject('categoryPresenter', 'App\Presenters\CategoryPresenter')

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

@@ -39,6 +39,9 @@
                                     <td>
                                         <a href='{{ route("backend.category.edit", ["id" => $id]) }}' class='btn btn-info btn-xs'>
                                             <i class="fa fa-pencil"></i> 修改</a>
+                                        <a href='{{ route("backend.category.set-nav", ["id" => $id]) }}' class='btn btn-info btn-xs'>
+                                            设为导航
+                                        </a>
                                         <a data-href='{{ route("backend.category.destroy", ["id" => $id]) }}'
                                            class='btn btn-danger btn-xs category-delete'><i class="fa fa-trash-o"></i> 删除</a>
                                     </td>

+ 0 - 2
resources/views/backend/navigation/index.blade.php

@@ -64,10 +64,8 @@
                                         @endif
                                     </td>
                                     <td>
-                                        @if ($navigation->nav_type == 0)
                                         <a href='{{ route("backend.navigation.edit", ["id" => $navigation->id]) }}' class='btn btn-info btn-xs'>
                                             <i class="fa fa-pencil"></i> 修改</a>
-                                        @endif
                                         <a data-href='{{ route("backend.navigation.destroy", ["id" => $navigation->id]) }}'
                                            class='btn btn-danger btn-xs navigation-delete'><i class="fa fa-trash-o"></i> 删除</a>
                                     </td>

+ 0 - 0
resources/views/default/article.blade.php