提交代码
This commit is contained in:
60
app/Http/Controllers/ArticleController.php
Normal file
60
app/Http/Controllers/ArticleController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Advert;
|
||||
use App\Models\Article;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\View;
|
||||
|
||||
class ArticleController extends Controller
|
||||
{
|
||||
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
view()->share('nav', 3);
|
||||
view()->share('app_title', '教程资讯');
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$category_id = $request->category_id;
|
||||
$page = $request->page ?? '';
|
||||
|
||||
$lists = Article::where('hot', 0)->when($category_id, function ($q) use ($category_id) {
|
||||
$q->where('category_id', $category_id);
|
||||
})->orderBy('created_at', 'desc')->paginate();
|
||||
|
||||
if ($page > 1) {
|
||||
if ($lists->isNotEmpty()) {
|
||||
$html = response(view('articles.more', compact('lists')))->getContent();
|
||||
return $this->success($html);
|
||||
} else {
|
||||
return $this->error('没有数据了');
|
||||
}
|
||||
} else {
|
||||
$categorys = Category::where('type', 'article')->where('parent_id', 0)->get();
|
||||
$adverts = Advert::where('channel', 'article')->orderBy('sort', 'asc')->take(5)->get(); //banner
|
||||
$hot = Article::where('hot', 1)->orderBy('id', 'desc')->first();
|
||||
return view('articles.index', compact('lists', 'categorys', 'adverts', 'category_id', 'hot'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function show(Article $article)
|
||||
{
|
||||
$article->increment('clicks');
|
||||
$lists = Article::where('category_id', $article->category_id)->inRandomOrder()->take(2)->get();
|
||||
return view('articles.show', compact('article', 'lists'));
|
||||
}
|
||||
|
||||
public function more(Request $request)
|
||||
{
|
||||
$category_id = $request->category_id;
|
||||
|
||||
$lists = Article::when($category_id, function ($q) use ($category_id) {
|
||||
$q->where('category_id', $category_id);
|
||||
})->orderBy('created_at', 'desc')->paginate();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user