70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Advert;
|
|
use App\Models\Article;
|
|
use App\Models\Category;
|
|
|
|
class CategoryController extends Controller
|
|
{
|
|
|
|
/**
|
|
* 显示分类
|
|
*
|
|
* @param Category $category [description]
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View [type] [description]
|
|
*/
|
|
public function index(Category $category)
|
|
{
|
|
if ($category->type == Category::TYPE_SHOW && $category->article_id) {
|
|
return redirect("articles/".$category->article_id);
|
|
} else {
|
|
$directory = 'category';
|
|
$template = 'show';
|
|
if ($category->template) {
|
|
$template = $category->template;
|
|
}
|
|
|
|
$pageCount = 15;//每页显示数量
|
|
//人才
|
|
if ($category->type == Category::TYPE_PERSON) {
|
|
$directory = 'resume';
|
|
$pageCount = 100;
|
|
}
|
|
|
|
if ($category->id == 37) {
|
|
$categories = Category::get();
|
|
$children = array_merge([$category->id], getAllChild($categories, $category->id));
|
|
|
|
$articles = Article::query()
|
|
->whereHas('categories', function ($q) use ($children) {
|
|
$q->whereIn('id', $children);
|
|
})
|
|
->where('status', 1)
|
|
->latest('sort')
|
|
->latest('created_at')
|
|
->paginate($pageCount);
|
|
} else {
|
|
$articles = $category->relations(Category::TYPE_ARTICLE)
|
|
->where('status', 1)
|
|
->latest('sort')
|
|
->latest('created_at')
|
|
->paginate($pageCount);
|
|
}
|
|
|
|
|
|
$parent = $category;
|
|
if (! $category->getChildrenCount() && $category->parent) {
|
|
|
|
$parent = $category->parent;
|
|
}
|
|
|
|
$advert = Advert::where('category_id', 73)->first();
|
|
|
|
return view($directory.'.'.$template, compact('articles', 'category', 'parent', 'advert'));
|
|
}
|
|
}
|
|
|
|
}
|