Answer the question
In order to leave comments, you need to log in
Has anyone used the franzose/ClosureTable package for Laravel (5.1)?
I am using the franzose/ClosureTable package (in particular for the hierarchical category of products) for a multilingual site on Laravel 5.1.
The problem is that I can not get categories for a specific language. For example, for $category->getRoots()
retrieves all parent categories, but not for a specific language.
I would like to do something like this $category->lang()->getRoots()
, which would return all parent categories for a particular language.
I was able to implement this myself (but it does not work).
Added two columns to the standard migration file generated by the franzose/ClosureTable package:
$table->string('name');
$table->enum('lang', ['ru', 'en'])->default('ru');
public function scopeLang($query)
{
$lang = \App::getLocale();
return $query->where('lang', $lang);
}
$category->lang()->getRoots()
, it throws an error: $category->getRoots()->lang()
: $categories->getRoots()->where('lang', 'ru')
, then it works. $category->lang()->getRoots()
for the methods Ancestors, Direct descendants (children), Descendants, Siblings? To filter data by a specific language. Answer the question
In order to leave comments, you need to log in
Hello! For such logic, I would use a repository that would pass through the interface to the controller. For example:
EloquentCategoryRepository implements CategoryRepository {
public function __construct(Category $category)
{
$this->category = $category;
}
public function getRootsByLang($lang)
{
return $this->category->getRoots()->whereLang($lang)->get();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question