PHP 8.2.30
Preview: ServiceCategoryRepository.php Size: 4.97 KB
/home/byroehnu/easetack.com/app/Repositories/Admin/ServiceCategoryRepository.php

<?php

namespace App\Repositories\Admin;

use App\Http\Requests\Admin\ServiceCategoryStoreRequest;
use App\Http\Requests\Admin\ServiceCategoryUpdateRequest;
use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\Setting;
use App\Repositories\Traits\ModelRepositoryTraits;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class ServiceCategoryRepository
{
    use ModelRepositoryTraits;

    /**
     * Object model will be used to modify categories table
     */
    protected ServiceCategory $model;

    /**
     *  Constructor for Category repository
     */
    public function __construct(ServiceCategory $category)
    {
        $this->model = $category;
    }

    /**
     * Get search result with paginate
     */
    public function paginateSearchResult($search, array $sort = []): LengthAwarePaginator
    {
        $query = $this->model->with('content')->newQuery();

        // search category
        if (isset($search)) {
            $query->whereHas('contents', function ($q) use ($search) {
                $q->where('language_code', app()->getLocale())
                    ->where('title', 'like', '%' . $search . '%');
            });
        }

        // sort category
        if (isset($sort['column'])) {
            $query->orderBy($sort['column'], $sort['order']);
        }

        return $query->paginate(30)
            ->appends(array_filter([
                'search' => $search,
                'sort' => $sort,
                'lang' => app()->getLocale(),
            ]));
    }

    /**
     * Store category
     */
    public function store(ServiceCategoryStoreRequest $request): void
    {
        $category = $this->model->create($request->all());

        $languages = json_decode(Setting::pull('languages'), true);
        $content = array_map(function ($language) use ($request) {
            $langCode = $language['code'];

            return [
                'language_code' => $langCode,
                'title' => $request->input($langCode . '_title'),
            ];
        }, $languages);

        $category->contents()->createMany($content);
    }

    /**
     * Get edited data
     */
    public function getEditData(ServiceCategory $serviceCategory): array
    {
        $languages = json_decode(Setting::pull('languages'), true);
        $data = [
            'id' => $serviceCategory->id,
            'created_at' => $serviceCategory->created_at,
        ];

        foreach ($languages as $language) {
            $langCode = $language['code'];
            $data[$langCode . '_title'] = '';
        }

        foreach ($serviceCategory->contents as $content) {
            $langCode = $content->language_code;
            $data[$langCode . '_title'] = $content->title;
        }

        return $data;
    }

    /**
     * Update category
     */
    public function update(ServiceCategory $serviceCategory, ServiceCategoryUpdateRequest $request): void
    {
        if ($serviceCategory?->content?->title == 'Uncategorized') {
            throw new \Exception('The category cannot be updated.');
        }

        $languages = json_decode(Setting::pull('languages'), true);
        foreach ($languages as $language) {
            $langCode = $language['code'];

            $serviceCategory->contents()->updateOrCreate(
                ['language_code' => $langCode],
                [
                    'title' => $request[$langCode . '_title'],
                ],
            );
        }
    }

    /**
     * Bulk delete
     */
    public function bulkDelete(Request $request): void
    {
        $uncategorizedCategoryId = ServiceCategory::where('title', 'Uncategorized')->first()->id;
        if ($uncategorizedCategoryId) {
            // Exclude the "uncategorized" category ID from the list
            $categoriesToDelete = array_diff(explode(',', $request->ids), [$uncategorizedCategoryId]);

            if (empty($categoriesToDelete)) {
                // No categories to delete (excluding "uncategorized")
                return;
            }

            // Update posts assigned to the categories to the "uncategorized" category
            Service::whereIn('category_id', $categoriesToDelete)->update(['category_id' => $uncategorizedCategoryId]);

            //  Delete the categories (excluding "uncategorized")
            ServiceCategory::whereIn('id', $categoriesToDelete)->delete();
        }
    }

    /**
     * Delete category
     *
     * @throws \Exception
     */
    public function destroy(ServiceCategory $serviceCategory): void
    {
        if ($serviceCategory->title == 'Uncategorized') {
            throw new \Exception('The category cannot be deleted.');
        }

        $uncategorizedCategoryId = ServiceCategory::where('title', 'Uncategorized')->first()?->id;

        // Update posts assigned to the categories to the "uncategorized" category
        Service::where('category_id', $serviceCategory->id)->update(['category_id' => $uncategorizedCategoryId]);
        //  Delete the categories
        $serviceCategory->delete();
    }
}

Directory Contents

Dirs: 0 × Files: 37

Name Size Perms Modified Actions
713 B lrw-rw-rw- 2025-04-08 04:12:56
Edit Download
4.90 KB lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
8.52 KB lrw-rw-rw- 2025-04-19 04:22:17
Edit Download
4.28 KB lrw-rw-rw- 2025-05-15 11:40:33
Edit Download
1.27 KB lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
3.81 KB lrw-rw-rw- 2025-04-08 04:12:56
Edit Download
2.42 KB lrw-rw-rw- 2025-04-08 04:12:56
Edit Download
1.11 KB lrw-rw-rw- 2025-04-08 04:12:56
Edit Download
1.58 KB lrw-rw-rw- 2025-04-22 07:44:34
Edit Download
1.18 KB lrw-rw-rw- 2025-05-05 10:37:43
Edit Download
11.35 KB lrw-rw-rw- 2025-04-19 04:42:55
Edit Download
5.59 KB lrw-rw-rw- 2025-05-15 11:24:11
Edit Download
982 B lrw-rw-rw- 2025-04-19 04:22:17
Edit Download
5.26 KB lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
7.94 KB lrw-rw-rw- 2025-05-08 11:48:26
Edit Download
1.40 KB lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
4.73 KB lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
8.77 KB lrw-rw-rw- 2025-04-19 04:22:17
Edit Download
7.70 KB lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
5.04 KB lrw-rw-rw- 2025-05-15 11:24:11
Edit Download
3.88 KB lrw-rw-rw- 2025-04-19 04:22:17
Edit Download
7.79 KB lrw-rw-rw- 2025-04-22 07:42:55
Edit Download
1.32 KB lrw-rw-rw- 2025-04-22 07:42:55
Edit Download
653 B lrw-rw-rw- 2025-04-19 04:22:17
Edit Download
3.79 KB lrw-rw-rw- 2025-05-04 10:44:46
Edit Download
2.27 KB lrw-rw-rw- 2025-04-26 10:16:33
Edit Download
4.97 KB lrw-rw-rw- 2025-05-15 11:24:11
Edit Download
8.42 KB lrw-rw-rw- 2025-04-19 04:22:17
Edit Download
723 B lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
2.42 KB lrw-rw-rw- 2025-04-08 04:12:56
Edit Download
1.19 KB lrw-rw-rw- 2025-04-08 04:12:56
Edit Download
7.19 KB lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
4.42 KB lrw-rw-rw- 2025-05-01 06:16:57
Edit Download
1017 B lrw-rw-rw- 2025-04-08 04:12:56
Edit Download
2.63 KB lrw-rw-rw- 2025-04-13 07:02:31
Edit Download
3.51 KB lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
2.51 KB lrw-rw-rw- 2025-04-08 04:12:56
Edit Download

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