A
A
Andrey Boychenko2019-04-21 03:01:30
Laravel
Andrey Boychenko, 2019-04-21 03:01:30

Laravel GroupBY?

Goodnight! I don't quite understand how to implement the following. There are categories, there are products, there are characteristics that are tied to products. I need to group products in different categories, according to different characteristics.
The user, adding a category, chooses by which characteristic the grouping will go.
The table looks like this
: categories:
id
title
parent
group_by (characteristic id)
Accordingly, there is a field with a category in the table with products.
products:
id
title
category
...
There is also a table with characteristics and values ​​of characteristics
specifications:
id,
title
specifications_values:
id,
value,
specification_id
And if, let's say, I would need to group values ​​by some field inside a table with products. No problem, everything is clear. But I don’t understand at all how I can group products by values ​​from the specifications_values ​​table, which are linked to the specifications table, in turn, the id of which is in the group_by field, of the table with categories.
When I declare a "relationship" of products with a category and try to group by value from the group_by field, then everything is fine.
This is what my model looks like

public function relatedCategory()
    {
        return $this->hasOne(ModelNomenclature::class, 'id', 'category');
    }

    public function getProducts()
    {
        return self::with('relatedCategory')->latest()->get();
    }

And here is the controller
public function index()
    {
        $data = [
            'nomenclature' => $this->nomenclature->selfList(),
            'products' => $this->products->getProducts()
        ];

        dd( $data['products']->groupBy('relatedCategory.group_by') );
    }

But if I add another "relationship" and select all values ​​from the specifications_values ​​table where the specification_id field is equal to the group_by value inside the categories table. Then all products are grouped as follows
Screen
5cbbafb3d2911709086054.png

I get this result by modifying the controller as follows. The relatedSpecsValues ​​values ​​are loaded correctly.
Model:
public function getProducts()
    {
        return self::with('relatedCategory.relatedSpecsValues')->latest()->get();
    }

Controller :
public function index()
    {
        $data = [
            'nomenclature' => $this->nomenclature->selfList(),
            'products' => $this->products->getProducts()
        ];

        dd($data['products']->groupBy('relatedCategory.relatedSpecsValues.value'));
    }

In addition, on my product page, when the page loads, only categories and subcategories are displayed.
When I click on a subcategory, I use ajax to load a list of products. Again, not fully loaded, because the so-called infinity scroll is implemented. And if just loading products is no problem, then what about loading grouped values? Since there can be more than 1k products in one category, I need the page to load quickly. It turns out that I need to asynchronously load all the values ​​​​by which the grouping will go, draw them and, after clicking on any value, load and draw N products, and then when scrolling down the page, load N more products.
Maybe I messed up with the database?
If possible, help with the solution of the problem and I will be even more glad if you explain everything to me so that I understand exactly the subtleties. The last thing I want is copy paste, simply because it's a road to nowhere, but sometimes that's all there is to it. In any case, I will be grateful for any kind of help and advice!
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arthur, 2019-04-21
@ART_CORP

Producr::skip($skip)->take($limit)->get()
When scrolling, change the value of $skip

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question