Y
Y
Yevhenii K2021-02-24 16:50:28
Laravel
Yevhenii K, 2021-02-24 16:50:28

How to get many Many-to-Many relationships through one pivot table?

There is a table for linking product properties and their values.
60362322a3fd4040947558.png

public function features()
{
    return DB::table('feature_product_pivot')
        ->leftJoin('features', 'feature_product_pivot.feature_id',
                   '=', 'features.id')
        ->leftJoin('feature_values', 'feature_product_pivot.value_id',
                   '=', 'feature_values.id')
        ->leftJoin('feature_units', 'feature_product_pivot.unit_id',
                   '=', 'feature_units.id')
        ->where('product_id', $this->id)
        ->get();
}

Is it possible to implement a query using Eloquent and relationships?

At the moment, I am considering using an intermediate model **ProductFeature** for a given table that will be associated with a product.

What is the best way to implement this type of task?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Northern Lights, 2021-02-24
@php666

No join needed. Open the documentation and read how to use links.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question