59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Category;
|
|
use Livewire\Component;
|
|
|
|
class CategorySelectbox extends Component
|
|
{
|
|
public $categoryOptions = [];
|
|
public $categorySelected;
|
|
|
|
/**
|
|
* Prepare the component.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function mount($categorySelected)
|
|
{
|
|
$this->categoryOptions = Category::with('translations')
|
|
->whereNot('type', 'App\Models\Tag')
|
|
->get()
|
|
->sortBy(function ($category) {
|
|
// Use the translated name (accessor) for sorting, checking if translation exists
|
|
return $category->translation->name ?? __('Untitled category');
|
|
})
|
|
->mapWithKeys(function ($category) {
|
|
return [
|
|
$category->id => [
|
|
'category_id' => $category->id,
|
|
// Check if translation exists before trying to access its name property
|
|
'name' => $category->translation ? $category->translation->name : __('Category') . ' ' . __('id') . ' ' . $category->id . ' ' . $category->type,
|
|
]
|
|
];
|
|
})
|
|
->toArray();
|
|
|
|
$this->categorySelected = $categorySelected;
|
|
$this->updated();
|
|
}
|
|
|
|
|
|
/**
|
|
* When component is updated
|
|
*
|
|
* @return void
|
|
*/
|
|
public function updated()
|
|
{
|
|
$this->dispatch('categorySelected', $this->categorySelected);
|
|
}
|
|
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.category-selectbox');
|
|
}
|
|
}
|