PHP 8.2.30
Preview: BlogRepository.php Size: 2.62 KB
/home/byroehnu/easetack.com/app/Repositories/Frontend/BlogRepository.php

<?php

namespace App\Repositories\Frontend;

use App\Models\Comment;
use App\Models\Post;
use Illuminate\Http\Request;

class BlogRepository
{
    protected Post $model;

    public function __construct(Post $post)
    {
        $this->model = $post;
    }

    public function paginateSearchResult($search, array $filter = [])
    {
        $query = $this->model->with([
            'content',
            'category.content',
            'user',
        ])->withCount('comments')->newQuery();

        if (! empty($search)) {
            $query->whereHas('contents', function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%")
                    ->orWhere('title', 'like', "%{$search}%");
            })->orWhereHas('category.contents', function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%");
            })->orWhereHas('tags', function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%");
            });
        }

        if (! empty($filter['category'])) {
            $query->whereHas('category.content', function ($q) use ($filter) {
                $q->where('title', $filter['category']);
            });
        }

        if (! empty($filter['tag'])) {
            $query->withAnyTags([$filter['tag']]);
        }

        if (! empty($filter['author'])) {
            $query->whereHas('user', function ($q) use ($filter) {
                $q->where('id', $filter['author']);
            });
        }

        return $query->where('status', '1')->paginate(6);
    }

    /**
     * Show blog post
     */
    public function show($slug): mixed
    {
        $post = $this->model->with(['content', 'category.content', 'comments', 'user'])->where('slug', $slug)->withCount('comments')->first();
        if (! $post) {
            abort(404);
        }

        return $post;
    }

    /**
     * Post comment store
     */
    public function storeComment(Request $request): void
    {
        Comment::create([
            'post_id' => $request->post_id,
            'comment_parent' => $request->comment_parent,
            'comment_content' => $request->comment,
            'comment_author_website' => $request->website,
            'comment_author_email' => $request->email,
            'comment_author_name' => $request->full_name,
        ]);
    }

    /**
     * Get published blog
     *
     * @return mixed
     */
    public function getPublishedBlogs()
    {
        return $this->model->where('status', '1')
            ->with('content', 'user', 'category.content')
            ->take(10)
            ->inRandomOrder()
            ->latest()
            ->get();
    }
}

Directory Contents

Dirs: 0 × Files: 9

Name Size Perms Modified Actions
2.62 KB lrw-rw-rw- 2025-04-19 04:22:15
Edit Download
8.26 KB lrw-rw-rw- 2025-05-15 11:24:11
Edit Download
1.45 KB lrw-rw-rw- 2025-04-19 04:22:15
Edit Download
6.38 KB lrw-rw-rw- 2025-05-08 12:05:13
Edit Download
2.52 KB lrw-rw-rw- 2025-04-26 11:30:31
Edit Download
1.77 KB lrw-rw-rw- 2025-04-15 10:33:57
Edit Download
829 B lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
5.46 KB lrw-rw-rw- 2025-04-24 11:21:09
Edit Download
3.50 KB lrw-rw-rw- 2025-04-13 07:02:31
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).