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;
}
}
}