HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: //home/armgroup/libs/app/Http/Livewire/Admin/Product/SelectCategory.php
<?php

namespace App\Http\Livewire\Admin\Product;

use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class SelectCategory extends Component
{
    use ConvertEmptyStringsToNull, General;

    public $categories;

    public $parent_id = null;

    public $parent_name = null;

    public $product;

    public function mount($categories)
    {
        $this->categories = $categories;
    }

    public function render()
    {
        return view('livewire.admin.product.select-category');
    }

    public function goToCreate($category_id)
    {
        if ($this->product) {
            DB::table('categorizables')->where('categorizable_type', Product::class)->where('categorizable_id', $this->product->id)->update(['is_main' => 0]);
            $this->product->categories()->syncWithoutDetaching([$category_id => ['is_main' => 1]]);

            return redirect()->route('admin.products.edit', $this->product);
        }

        return redirect()->route('admin.products.create', ['category' => $category_id]);
    }

    public function changeCategories(Category $category)
    {
        $this->parent_id = $category->parent_id;
        $this->parent_name = $category->name;
        $this->categories = Category::select('id', 'name', 'slug')
            ->withCount('activeChildren')
            ->where('parent_id', $category->id)
            ->where('type', Product::class)
            ->get();
    }

    public function changeToUpCategories($parent_id = null)
    {
        if ($parent_id) {
            $category = Category::where('id', $parent_id)->first();
            $this->categories = Category::select('id', 'name', 'slug')
                ->withCount('activeChildren')
                ->where('parent_id', $this->parent_id)
                ->where('type', Product::class)
                ->get();

            $this->parent_id = $category->parent_id;
            $this->parent_name = $category->name;
        } else {
            $this->categories = Category::select('id', 'name', 'slug')
                ->withCount('activeChildren')
                ->whereNull('parent_id')
                ->where('type', Product::class)
                ->get();

            $this->parent_id = null;
            $this->parent_name = null;
        }
    }
}