Files
ahrommag/app/Livewire/Home.php
2025-11-16 12:43:07 +03:30

119 lines
4.0 KiB
PHP

<?php
namespace App\Livewire;
use App\Models\Blog;
use Livewire\Component;
use App\Models\Category;
use Illuminate\Http\Request;
use Livewire\Attributes\Layout;
use Illuminate\Support\Facades\Http;
class Home extends Component
{
public $image;
public $blogs;
public $news;
public $blogs2;
public $blog_array = [];
public $news_array = [];
public $categories = [];
public $categorieNews = [];
public $lastNewsPosts = [];
public $lastNewsArticles = [];
public $lightmags = [];
public $lightnews = [];
public $selectedCategoryPosts = null;
public $selectedCategoryNews = null;
public $selectedRoutePosts = null;
public $selectedRouteNews = null;
public function mount(Request $request)
{
$allBlogs = Blog::where('status', 1)->orderBy('published_at', 'desc')->get();
$this->lightmags = $allBlogs->whereNull('notBlog')->take(50);
$this->lightnews = $allBlogs->where('notBlog', 1)->take(50);
$allCategroyBuilder = Category::where('status', 1);
$allCategroyNewsBuilder = Category::where('status', 1);
$this->blogs2 = Blog::where('status', 1)->where('notBlog', null)->where('chosen', 1)->orderBy('created_at', 'desc')->get()->take(5);
$this->lastNewsPosts = $allBlogs->whereNull('notBlog')->take(4);
$this->lastNewsArticles = $allBlogs->where('notBlog', 1)->take(4);
$this->blogs = $allBlogs->where('chosen', 0)->where('notBlog', null);
$this->news = $allBlogs->where('chosen', 0)->where('notBlog', 1);
$this->categories = $allCategroyBuilder->with(['blogs' => function ($q) {
$q->where('status', 1);
}])->where('parent_below', null)->where('parent_id', null)->where('parent', null)->get();
$this->categorieNews = $allCategroyNewsBuilder->where('is_news', 1)->where('parent_below', null)->where('parent_id', '!=', null)->where('parent', null)->get();
}
public function selectCategory($categoryId, $type = 'posts')
{
$category = Category::find($categoryId);
if ($type === 'posts') {
$this->selectedCategoryPosts = $categoryId;
if (isset($category)) {
$this->selectedRoutePosts = route('CategoryBlog.index', ['slug' => $category->slug]) . "/";
}
} else {
$this->selectedCategoryNews = $categoryId;
if (isset($category)) {
$this->selectedRouteNews = route('NewsCategory.show', ['slug' => $category->slug]) . "/";
}
}
}
public function resetCategory($type = 'posts')
{
$this->selectedRoutePosts = null;
$this->selectedRouteNews = null;
if ($type === 'posts') {
$this->selectedCategoryPosts = null;
} else {
$this->selectedCategoryNews = null;
}
}
public function getCategoryItems($categoryId = null, $type = 'posts')
{
if ($type === 'posts') {
$query = Blog::where('status', 1)->whereNull('notBlog');
if ($categoryId) {
$query->where(function($q) use ($categoryId) {
$q->where('category_id', $categoryId)
->orWhereJsonContains('category_array', (string)$categoryId);
});
}
return $query->orderByDesc('published_at')
->take(4)
->get();
} else {
$query = Blog::where('status', 1)->where('notBlog', 1);
if ($categoryId) {
$query->where(function($q) use ($categoryId) {
$q->where('category_id', $categoryId)
->orWhereJsonContains('category_array', (string)$categoryId);
});
}
return $query->orderByDesc('published_at')
->take(4)
->get();
}
}
#[Layout('components.layouts.app')]
public function render()
{
return view('livewire.home');
}
}