fixed category calculate

This commit is contained in:
vedatakd 2021-03-18 10:11:34 +03:00
parent 61a7bbcad6
commit 994c32fdeb
2 changed files with 45 additions and 40 deletions

View File

@ -24,6 +24,7 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
->orderBy('sort_order') ->orderBy('sort_order')
->get(); ->get();
} }
public function getSubCatById($id) public function getSubCatById($id)
{ {
$cats = $this->model->newQuery() $cats = $this->model->newQuery()
@ -108,7 +109,8 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
public function getLevelById($id) public function getLevelById($id)
{ {
return count($this->getParentCategoryById($id)); $parents = $this->getParentCategoryById($id);
return (is_array($parents)) ? count($parents) : null;
} }
public function getCategoriesByName($keyword) public function getCategoriesByName($keyword)
@ -133,45 +135,45 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
return $this->model->withTrashed()->newQuery()->whereNotNull('deleted_at')->get(); return $this->model->withTrashed()->newQuery()->whereNotNull('deleted_at')->get();
} }
public function getMainAndSubCats() public function getMainAndSubCats()
{ {
$dBName = 'cats_category'; $dBName = 'cats_category';
$dBNamet = $dBName . '_translations'; $dBNamet = $dBName . '_translations';
$catsDB = DB::table($dBName . ' as c1') $catsDB = DB::table($dBName . ' as c1')
->select( ->select(
'c1.id', 'c1.id',
'c1.slug', 'c1.slug',
'c1.parent_category_id', 'c1.parent_category_id',
'c1.icon_id', 'c1.icon_id',
't1.name', 't1.name',
'c2.id as c2_id', 'c2.id as c2_id',
'c2.slug as c2_slug', 'c2.slug as c2_slug',
'c2.parent_category_id as c2_parent_category_id', 'c2.parent_category_id as c2_parent_category_id',
't2.name as c2_name', 't2.name as c2_name',
'file.id as file_id' 'file.id as file_id'
) )
->leftJoin($dBName . ' as c2', function ($join) { ->leftJoin($dBName . ' as c2', function ($join) {
$join->on('c2.parent_category_id', '=', 'c1.id') $join->on('c2.parent_category_id', '=', 'c1.id')
->whereNull('c2.deleted_at'); ->whereNull('c2.deleted_at');
}) })
->leftJoin($dBNamet . ' as t1', function ($join) use ($dBNamet) { ->leftJoin($dBNamet . ' as t1', function ($join) use ($dBNamet) {
$join->on('c1.id', '=', 't1.entry_id') $join->on('c1.id', '=', 't1.entry_id')
->where('t1.locale', Request()->session()->get('_locale', setting_value('streams::default_locale'))); ->where('t1.locale', Request()->session()->get('_locale', setting_value('streams::default_locale')));
}) })
->leftJoin($dBNamet . ' as t2', function ($join) use ($dBNamet) { ->leftJoin($dBNamet . ' as t2', function ($join) use ($dBNamet) {
$join->on('c2.id', '=', 't2.entry_id') $join->on('c2.id', '=', 't2.entry_id')
->where('t2.locale', Request()->session()->get('_locale', setting_value('streams::default_locale'))); ->where('t2.locale', Request()->session()->get('_locale', setting_value('streams::default_locale')));
}) })
->leftJoin('files_files as file', 'c1.icon_id', 'file.id') ->leftJoin('files_files as file', 'c1.icon_id', 'file.id')
->whereNull('c1.deleted_at') ->whereNull('c1.deleted_at')
->whereNull('c1.parent_category_id') ->whereNull('c1.parent_category_id')
->orderBy('c1.sort_order') ->orderBy('c1.sort_order')
->orderBy('c2.sort_order') ->orderBy('c2.sort_order')
->get(); ->get();
$cats = collect([]); $cats = collect([]);
$cats->subcats = $catsDB; $cats->subcats = $catsDB;
$cats->maincats = $catsDB->unique('id'); $cats->maincats = $catsDB->unique('id');
return $cats; return $cats;
} }
} }

View File

@ -44,10 +44,13 @@ class CalculateCategoryLevel
$level = $categoryRepository->getLevelById($category_id); $level = $categoryRepository->getLevelById($category_id);
DB::table('cats_category')->where('id', $category_id) if($level)
->update(array( {
'level' => $level, DB::table('cats_category')->where('id', $category_id)
'level_at' => now(), ->update(array(
)); 'level' => $level,
'level_at' => now(),
));
}
} }
} }