-
Notifications
You must be signed in to change notification settings - Fork 24
/
PostController.php
89 lines (70 loc) · 2.5 KB
/
PostController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Firefly\FilamentBlog\Http\Controllers;
use Firefly\FilamentBlog\Facades\SEOMeta;
use Firefly\FilamentBlog\Models\NewsLetter;
use Firefly\FilamentBlog\Models\Post;
use Firefly\FilamentBlog\Models\ShareSnippet;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index(Request $request)
{
SEOMeta::setTitle('Blog | '.config('app.name')) ;
$posts = Post::query()->with(['categories', 'user', 'tags'])
->published()
->paginate(10);
return view('filament-blog::blogs.index', [
'posts' => $posts,
]);
}
public function allPosts()
{
SEOMeta::setTitle('All posts | '.config('app.name')) ;
$posts = Post::query()->with(['categories', 'user'])
->published()
->paginate(20);
return view('filament-blog::blogs.all-post', [
'posts' => $posts,
]);
}
public function search(Request $request)
{
SEOMeta::setTitle('Search result for '.$request->get('query'));
$request->validate([
'query' => 'required',
]);
$searchedPosts = Post::query()
->with(['categories', 'user'])
->published()
->whereAny(['title', 'sub_title'], 'like', '%'.$request->get('query').'%')
->paginate(10)->withQueryString();
return view('filament-blog::blogs.search', [
'posts' => $searchedPosts,
'searchMessage' => 'Search result for '.$request->get('query'),
]);
}
public function show(Post $post)
{
SEOMeta::setTitle($post->seoDetail?->title);
SEOMeta::setDescription($post->seoDetail?->description);
SEOMeta::setKeywords($post->seoDetail->keywords ?? []);
$shareButton = ShareSnippet::query()->active()->first();
$post->load(['user', 'categories', 'tags', 'comments' => fn ($query) => $query->approved(), 'comments.user']);
return view('filament-blog::blogs.show', [
'post' => $post,
'shareButton' => $shareButton,
]);
}
public function subscribe(Request $request)
{
$request->validate([
'email' => 'required|email|unique:news_letters,email',
], [
'email.unique' => 'You have already subscribed',
]);
NewsLetter::create([
'email' => $request->email,
]);
return back()->with('success', 'You have successfully subscribed to our news letter');
}
}