# 优化
## 后台
### 文章加分类等条件筛选
~~~php
...
$grid->filter(function (Grid\Filter $filter) {
$articleCategoryModel = new ArticleCategory();
// 更改为 panel 布局
$filter->panel();
// 展开过滤器
$filter->expand();
$filter->equal('id')
->width(1);
$filter->like('title', '标题')
->width(2);
$filter->equal('category_id', '类别')
->select($articleCategoryModel->selectOptions())
->width(2);
$filter->between('created_at', '添加时间')
->datetime()
->width(4);
});
...
~~~
### 修改主页内容
`app/Admin/Controllers/HomeController.php`
~~~php
use App\Admin\Metrics\NewArticles;
public function index(Content $content)
{
return $content
->header('仪表盘')
->description()
->body(function (Row $row) {
$row->column(12, function (Column $column) {
$column->row(function (Row $row) {
$row->column(6, new NewArticles());
});
});
});
}
~~~
卡片类文件
`app/Admin/Metrics/NewArticles.php`
~~~php
use App\Models\Article;
use Illuminate\Support\Carbon;
protected function init()
{
parent::init();
$this->title('新增文章');
$this->dropdown([
'7' => '过去7天',
'30' => '过去一个月',
'365' => '过去一年',
]);
}
public function handle(Request $request)
{
switch ($request->get('option')) {
case '365':
$this->withContent(Article::whereBetween('created_at', [
Carbon::now()
->subDays(365), today()
])
->count());
break;
case '30':
$this->withContent(Article::whereBetween('created_at', [
Carbon::now()
->subDays(30), today()
])
->count());
break;
case '7':
default:
$this->withContent(Article::whereBetween('created_at', [
Carbon::now()
->subDays(7), today()
])
->count());
}
}
public function withChart(array $data)
{
return $this->chart([
'series' => [
[
'name' => $this->title,
'data' => $data,
],
],
]);
}
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
<span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
);
}
~~~
## 前台
### 页脚
页脚的数据和页眉不一致 修改 `app/Http/ViewComposers/ArticleCategoryComposer.php`
~~~php
public function compose(View $view)
{
$articleCategoryModel = new ArticleCategory();
$nav = ArticleCategory::where('parent_id', 0)
->orderBy('order', 'desc')
->get();
//将文章和分类合并 并排序
if ($nav->isNotEmpty()) {
foreach ($nav as $article_category) {
$article_category->son = $articleCategoryModel->handle_category_data($article_category->articles, $article_category->children);
}
}
$nav_foot = ArticleCategory::where('parent_id', 0)
->orderBy('order')
->get();
//将文章和分类合并 并排序
if ($nav_foot->isNotEmpty()) {
foreach ($nav_foot as $article_category) {
$articles_footer = $article_category->articles()
->where('is_footer', 1)
->get();
$article_category->son = $articleCategoryModel->handle_category_data($articles_footer, $article_category->children);
}
}
$view->with(compact('nav', 'nav_foot'));
}
~~~
修改 `resources/views/index/layouts/_footer.blade.php`
~~~php
...
@foreach($nav_foot as $category)
...
@foreach($category->son as $item)
...
@endforeach
...
@endforeach
...
~~~
### 去掉没有用的代码