Files
new_haai/app/Http/Controllers/ArticleController.php

55 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Article;
use App\Models\Category;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
//文章列表
public function index(Category $category)
{
$articles = Article::where('category_id', $category->id)
->orderBy('created_at', 'desc')
->paginate(5);
return view('articles.index', compact('articles', 'category'));
}
//显示详情
public function show(Article $article)
{
if ($article->url) {
return redirect($article->url);
}
$category = $article->category;
$next = Article::where('id', '>', $article->id)
->where('category_id', $category->id)
->where('status', 1)
->first();
$parent = getTopCate($category->id);
return view('articles.show', compact('article', 'category', 'next', 'parent'));
}
//搜索
public function search(Request $request)
{
$title = $request->title;
$articles = Article::where('status', 1)
->when($title, function ($q) use ($title) {
$q->where('title', 'like', "%{$title}%");
})
->paginate();
return view('articles.search', compact('articles'));
}
}