M
M
Mokhirjon Naimov2015-07-03 15:37:18
Laravel
Mokhirjon Naimov, 2015-07-03 15:37:18

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');

And added to the model:
public function scopeLang($query)
{
        $lang = \App::getLocale();

        return $query->where('lang', $lang);
}

But it doesn't help, if used like this $category->lang()->getRoots(), it throws an error:
Call to undefined method Franzose\ClosureTable\Extensions\QueryBuilder::getRoots()
and if so$category->getRoots()->lang() :
Call to undefined method Franzose\ClosureTable\Extensions\Collection::lang()
But if directly set the conditions like this $categories->getRoots()->where('lang', 'ru'), then it works.
And finally, the question is how to implement the view construction $category->lang()->getRoots()for the methods Ancestors, Direct descendants (children), Descendants, Siblings? To filter data by a specific language.
I tried to figure it out myself through the source code of the package, but I don’t have enough mind where to write what ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jan Ivanov, 2015-07-05
@zvermafia

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

The same applies to other methods.
If you do not want to use a repository, then instead of scope, you need to extend the QueryBuilder with your own methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question