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

<?php

namespace App\Repositories\Frontend;

use App\Models\CaseStudy;
use App\Models\Page;
use App\Models\Portfolio;
use App\Models\Post;
use App\Models\Service;
use App\Models\Team;
use Illuminate\Support\Facades\DB;

class SearchRepository
{
    protected Post $postModel;

    protected Page $pageModel;

    protected Portfolio $portfolioModel;

    protected Service $serviceModel;

    protected CaseStudy $caseStudyModel;

    protected Team $teamModel;

    public function __construct(Post $post, Page $page, Portfolio $portfolio, Service $service, CaseStudy $caseStudy, Team $team)
    {
        $this->postModel = $post;
        $this->pageModel = $page;
        $this->portfolioModel = $portfolio;
        $this->serviceModel = $service;
        $this->caseStudyModel = $caseStudy;
        $this->teamModel = $team;
    }

    public function searchResult($search)
    {
        if (empty($search)) {
            return collect();
        }

        // Search information
        $posts = $this->searchPosts($search);

        $pages = $this->searchPages($search);

        $portfolios = $this->searchPortfolios($search);

        $services = $this->searchServices($search);

        $caseStudies = $this->searchCaseStudies($search);

        $teams = $this->searchTeams($search);

        $products = $this->searchProducts($search);

        $result = $posts->concat($pages)->concat($portfolios)->concat($services)->concat($caseStudies)->concat($teams)->concat($products);

        $result->each(function ($item) {
            if (isset($item->post_id)) {
                $item->type = 'post';
                $item->route = 'blog';
                $item->id = $item->post_id;
            } elseif (isset($item->page_id)) {
                $item->type = 'page';
                $item->route = '';
                $item->id = $item->page_id;
            } elseif (isset($item->portfolio_id)) {
                $item->type = 'portfolio';
                $item->route = 'portfolio';
                $item->id = $item->portfolio_id;
            } elseif (isset($item->service_id)) {
                $item->type = 'service';
                $item->route = 'service';
                $item->id = $item->service_id;
            } elseif (isset($item->case_study_id)) {
                $item->type = 'caseStudy';
                $item->route = 'case-study';
                $item->id = $item->case_study_id;
            } elseif (isset($item->team_id)) {
                $item->type = 'team';
                $item->route = 'team';
                $item->id = $item->team_id;
            } elseif (isset($item->product_id)) {
                $item->type = 'product';
                $item->route = 'product';
                $item->id = $item->product_id;
            }
        });

        return $result;
    }

    private function searchPosts($search)
    {
        return DB::table('post_contents')
            ->join('posts', 'post_contents.post_id', '=', 'posts.id')
            ->where('post_contents.title', 'like', "%{$search}%")
            ->select('posts.id as post_id', 'posts.slug', 'post_contents.title', 'post_contents.language_code')
            ->get();
    }

    private function searchPages($search)
    {
        return DB::table('page_contents')
            ->join('pages', 'page_contents.page_id', '=', 'pages.id')
            ->where('page_contents.title', 'like', "%{$search}%")
            ->select('pages.id as page_id', 'pages.slug', 'page_contents.title', 'page_contents.language_code')
            ->get();
    }

    private function searchPortfolios($search)
    {
        return DB::table('portfolio_contents')
            ->join('portfolios', 'portfolio_contents.portfolio_id', '=', 'portfolios.id')
            ->where('portfolio_contents.title', 'like', "%{$search}%")
            ->select('portfolios.id as portfolio_id', 'portfolios.slug', 'portfolio_contents.title', 'portfolio_contents.language_code')
            ->get();
    }

    private function searchServices($search)
    {
        return DB::table('service_contents')
            ->join('services', 'service_contents.service_id', '=', 'services.id')
            ->where('service_contents.title', 'like', "%{$search}%")
            ->select('services.id as service_id', 'services.slug', 'service_contents.title', 'service_contents.language_code')
            ->get();
    }

    private function searchCaseStudies($search)
    {
        return DB::table('case_study_contents')
            ->join('case_studies', 'case_study_contents.case_study_id', '=', 'case_studies.id')
            ->where('case_study_contents.title', 'like', "%{$search}%")
            ->select('case_studies.id as case_study_id', 'case_studies.slug', 'case_study_contents.title', 'case_study_contents.language_code')
            ->get();
    }

    private function searchProducts($search)
    {
        return DB::table('product_contents')
            ->join('products', 'product_contents.product_id', '=', 'products.id')
            ->where('product_contents.title', 'like', "%{$search}%")
            ->select('products.id as product_id', 'products.slug', 'product_contents.title', 'product_contents.language_code')
            ->get();
    }


    private function searchTeams($search)
    {
        return DB::table('team_contents')
            ->join('teams', 'team_contents.team_id', '=', 'teams.id')
            ->where('team_contents.title', 'like', "%{$search}%")
            ->select('teams.id as team_id', 'teams.slug', 'team_contents.title', 'team_contents.language_code')
            ->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).