first push
This commit is contained in:
50
app/Http/Controllers/ArticleController.php
Normal file
50
app/Http/Controllers/ArticleController.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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('status', 1)->first();
|
||||
|
||||
return view('articles.show', compact('article', 'category', 'next'));
|
||||
}
|
||||
|
||||
//搜索
|
||||
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'));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user