Add (news bites)

This commit is contained in:
2023-11-21 19:18:11 +08:00
parent 2e38a4120c
commit 3210563e42
37 changed files with 1581 additions and 153 deletions

View File

@@ -4,6 +4,7 @@
use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Models\RssPost;
use Artesaos\SEOTools\Facades\SEOMeta;
use Artesaos\SEOTools\Facades\SEOTools;
use GrahamCampbell\Markdown\Facades\Markdown;
@@ -18,13 +19,15 @@ public function home(Request $request)
// $query->whereNotIn('id', [$featured_post?->id]);
// })->where('status', 'publish')->orderBy('published_at', 'desc')->limit(5)->get();
$featured_posts = Post::where('status', 'publish')->where('published_at', '<=', now())->orderBy('published_at', 'desc')->limit(3)->get();
// $featured_posts = Post::where('status', 'publish')->where('published_at', '<=', now())->orderBy('published_at', 'desc')->limit(3)->get();
$latest_posts = Post::where(function ($query) use ($featured_posts) {
$query->whereNotIn('id', $featured_posts->pluck('id')->toArray());
})->where('status', 'publish')->where('published_at', '<=', now())->orderBy('published_at', 'desc')->limit(10)->get();
// $latest_posts = Post::where(function ($query) use ($featured_posts) {
// $query->whereNotIn('id', $featured_posts->pluck('id')->toArray());
// })->where('status', 'publish')->where('published_at', '<=', now())->orderBy('published_at', 'desc')->limit(10)->get();
return response(view('front.welcome', compact('featured_posts', 'latest_posts')), 200);
$rss_posts = RssPost::where('status', 'published')->orderBy('published_at', 'desc')->paginate(30);
return response(view('front.welcome', compact('rss_posts')), 200);
}
public function terms(Request $request)

View File

@@ -4,8 +4,7 @@
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Post;
use App\Models\PostCategory;
use App\Models\RssPost;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Http\Request;
use JsonLd\Context;
@@ -13,15 +12,24 @@
class FrontListController extends Controller
{
public function search(Request $request)
{
if (is_empty($request->input('query'))) {
return redirect()->back();
}
return redirect()->to(route('front.search.results',['query' => $request->input('query')]));
}
public function searchResults(Request $request, $query)
{
$page_type = 'search';
$query = $request->get('query', '');
$query = strtolower($query);
$breadcrumbs = collect([
['name' => 'Home', 'url' => route('front.home')],
['name' => 'Search', 'url' => null],
['name' => $query, 'url' => url()->current()],
['name' => 'News Bites', 'url' => route('front.search.results',['query' => $query])],
['name' => $query, 'url' => null],
]);
$title = 'Latest News about '.ucwords($query).' in FutureWalker';
@@ -32,14 +40,13 @@ public function search(Request $request)
SEOTools::jsonLd();
SEOTools::setTitle($title, false);
// Use full-text search capabilities of your database
// For example, using MySQL's full-text search with MATCH...AGAINST
$posts = Post::with('category')
->where('status', 'publish')
->whereRaw("to_tsvector('english', title || ' ' || bites) @@ to_tsquery('english', ?)", [str_replace(' ', ' & ', $query)])
->where('published_at', '<=', now())
->orderBy('published_at', 'desc')
->cursorPaginate(10);
$rss_posts = RssPost::with('category')
->where('status', 'published')
->whereRaw("to_tsvector('english', title || ' ' || bites || ' ' || body) @@ plainto_tsquery('english', ?)", [trim(preg_replace('/\s+/', ' ', $query))])
->where('published_at', '<=', now())
->orderBy('published_at', 'desc')
->cursorPaginate(60);
// breadcrumb json ld
$listItems = [];
@@ -55,7 +62,7 @@ public function search(Request $request)
'itemListElement' => $listItems,
]);
return view('front.post_list', compact('posts', 'breadcrumbs', 'breadcrumb_context', 'title', 'page_type'));
return view('front.rss_post_list', compact('rss_posts', 'breadcrumbs', 'breadcrumb_context', 'title', 'page_type'));
}
public function index(Request $request)
@@ -64,10 +71,10 @@ public function index(Request $request)
$breadcrumbs = collect([
['name' => 'Home', 'url' => route('front.home')],
['name' => 'Latest News', 'url' => null], // or you can set a route for Latest News if there's a specific one
['name' => 'News Bites', 'url' => null], // or you can set a route for Latest News if there's a specific one
]);
$title = 'Latest News from FutureWalker';
$title = 'Latest News Bites from FutureWalker';
SEOTools::metatags();
SEOTools::twitter();
@@ -75,10 +82,10 @@ public function index(Request $request)
SEOTools::jsonLd();
SEOTools::setTitle($title, false);
$posts = Post::with('category')->where('status', 'publish')
$rss_posts = RssPost::with('category')->where('status', 'published')
->where('published_at', '<=', now())
->orderBy('published_at', 'desc')
->cursorPaginate(10) ?? collect();
->cursorPaginate(60) ?? collect();
// breadcrumb json ld
$listItems = [];
@@ -90,52 +97,46 @@ public function index(Request $request)
];
}
//dd($posts);
//dd($rss_posts);
$breadcrumb_context = Context::create('breadcrumb_list', [
'itemListElement' => $listItems,
]);
return view('front.post_list', compact('posts', 'breadcrumbs', 'breadcrumb_context', 'page_type'));
return view('front.rss_post_list', compact('rss_posts', 'breadcrumbs', 'breadcrumb_context', 'page_type'));
}
public function category(Request $request, $category_slug)
{
$page_type = 'default';
$page_type = 'category';
// Fetch the category by slug
$category = Category::where('slug', $category_slug)->first();
// Check if the category exists
if (! $category) {
abort(404, 'Category not found');
if (is_null($category))
{
abort(404);
}
// Breadcrumb logic
$breadcrumbs = collect([['name' => 'Home', 'url' => route('front.home')]]);
foreach ($category->ancestors as $ancestor) {
$breadcrumbs->push(['name' => $ancestor->name, 'url' => route('front.category', $ancestor->slug)]);
}
$breadcrumbs->push(['name' => $category->name, 'url' => route('front.category', $category->slug)]);
// Get the IDs of the category and its descendants
$categoryIds = $category->descendants->pluck('id')->push($category->id);
$breadcrumbs = collect([
['name' => 'Home', 'url' => route('front.home')],
['name' => $category->name, 'url' => null],
]);
// Get the posts associated with these category IDs
$postIds = PostCategory::whereIn('category_id', $categoryIds)->pluck('post_id');
$posts = Post::whereIn('id', $postIds)
->where('published_at', '<=', now())
->where('status', 'publish')
->orderBy('published_at', 'desc')
->cursorPaginate(10);
$title = 'Latest News from FutureWalker';
$title = $category->name.' News from FutureWalker';
SEOTools::metatags();
SEOTools::twitter();
SEOTools::opengraph();
SEOTools::setTitle($title, false);
SEOTools::jsonLd();
SEOTools::setTitle($title, false);
$rss_posts = RssPost::with('category')->where('status', 'published')
->where('category_id', $category->id)
->where('published_at', '<=', now())
->orderBy('published_at', 'desc')
->cursorPaginate(60) ?? collect();
// breadcrumb json ld
$listItems = [];
@@ -147,10 +148,12 @@ public function category(Request $request, $category_slug)
];
}
//dd($rss_posts);
$breadcrumb_context = Context::create('breadcrumb_list', [
'itemListElement' => $listItems,
]);
return view('front.post_list', compact('category', 'posts', 'breadcrumbs', 'breadcrumb_context', 'page_type'));
return view('front.rss_post_list', compact('rss_posts', 'breadcrumbs', 'breadcrumb_context', 'page_type','category'));
}
}

View File

@@ -28,7 +28,7 @@ public function redirect(Request $request, $slug)
public function index(Request $request, $category_slug, $slug)
{
$post = Post::where('slug', $slug)->whereIn('status', ['publish','future'])->first();
$post = Post::where('slug', $slug)->whereIn('status', ['publish', 'future'])->first();
if (is_null($post)) {
return abort(404);