65 lines
1.5 KiB
PHP
65 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Menu;
|
|
|
|
use App\Repositories\BaseCachedRepository;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class MenuCachedRepository extends BaseCachedRepository implements MenuRepositoryInterface
|
|
{
|
|
public function __construct(
|
|
public MenuRepository $menuRepository
|
|
)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function getModel()
|
|
{
|
|
return $this->menuRepository->getModel();
|
|
}
|
|
|
|
public function filter(array $data)
|
|
{
|
|
$query = $this->model->query();
|
|
|
|
// Các điều kiện lọc
|
|
if (isset($data['is_active'])) {
|
|
$query->where('is_active', $data['is_active']);
|
|
}
|
|
|
|
if (isset($data['title'])) {
|
|
$query->where('title', 'like', '%'.$data['title'].'%');
|
|
}
|
|
|
|
// Dùng paginate thay vì get()
|
|
return $query->defaultOrder()->paginate(10);
|
|
}
|
|
|
|
public function getMenuParent($excludeId = null)
|
|
{
|
|
$query = $this->model->query();
|
|
|
|
if ($excludeId) {
|
|
$query->where('id', '!=', $excludeId);
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function store(array $data)
|
|
{
|
|
if (isset($data['parent_id']) && $data['parent_id']) {
|
|
$parent = $this->model->findOrFail($data['parent_id']);
|
|
return $parent->children()->create($data);
|
|
} else {
|
|
return $this->model->create($data);
|
|
}
|
|
}
|
|
|
|
public function getTree()
|
|
{
|
|
return $this->model->defaultOrder()->get()->toTree();
|
|
}
|
|
}
|