Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theblack68's avatar

Difficolt Query

Hi! I'm new in Laravel and I need some HELPS for a QUERY.

Sorry for my English

I have 3 tables:

  • Cables (belongs_to_many Features)

  • Features (belongs_to_many Cables)

  • Cable_Feature (Pivot) with pivot extra field value

I have created for everyone a Model with a relationship _(and all works fine...)_

  • The structure of Table Cables is (Id, Code)

  • The structure of Table Features is (Id, Name)

  • The structure of Pivot Table Cable_Feature is (cable_id, feature_id, value)

Now I need to build a query (to export in Excel with Maatwebsite/Excel). I need that every record have:

  • Cables.Code

  • Feature.Name

  • Value

I'm able to do it but my problem is that I need to have all on one line ...ES: Assume that we have 3 cables and 2 features. in database

Cables:

  • (id) 1, (code) 0031TN

  • (id) 2, (code) 0042GG

  • (id) 3, (code) 0025ZS

Features:

  • (id) 1, (name) PVC

  • (id) 2, (name) CM

And the situation in Cable_Feature is:

  • 1 - 1 - High Quality

  • 1 - 2 - 12

  • 2 - 1 - High Quality

  • 2 - 2 - 6

  • 3 - 1 Low Quality

  • 3 - 2 -11

Now I need to become with a query this result (1 for row)

  • 0031TN - High Quality - 12

  • 0042GG - High Quality - 6

  • 0025ZS - Low Quality - 11

Someone can help me please? ...thanks a lot!

0 likes
7 replies
aurawindsurfing's avatar

Hey @theblack68

Ok so this is at the moment ManyToMany relationship https://laravel.com/docs/5.8/eloquent-relationships#many-to-many

Your information is on pivot intermediate table so your result should look like this:

 $cables = Cable::all();

        foreach($cables as $cable){

            $cable_features = [];

            foreach ($cable->featue as $feature) {
                 array_push($cable_features,  $feature->pivot->name);
            }

            echo $cable->code .implode(' - ', $cable_features);
        }

Given that third column at your pivot table is called name

Hope it helps!

theblack68's avatar

First of all thanks for the help that you give me.

Explanation: I have write in italian (Cavi = Cables | Features = Caratteristiche)

I have resolved with a bit of code but I think is not a good super-solution (but works ;) )

The Maatwebsite/Excel package give you the ability to build $data to export. I have build all so:

$cavi = Cavo::with('famiglia', 'caratteristiche')->get();

        return $cavi->map(function($cavo) {
            foreach ($cavo->caratteristiche as $caratteristica) {
                if($caratteristica->id == 5 || $caratteristica->id == 7) {
                    $caratteristicheCavo[] = $caratteristica->pivot->valore;
                }
            }

            return $datiEplanArray = [
                'ParNumber' => "AGE" . $cavo->codice_articolo,
                'TypeNumber' => $cavo->famiglia->codice_articolo,
                'OrderNumber' => $cavo->codice_articolo,
                'Designation EN' => $cavo->famiglia->nome,
                'Description EN' => $cavo->famiglia->descrizione,
                'Document/Link 1' => '/ita/pdf/' . $cavo->famiglia->codice_articolo . '.pdf',
                'Description Doc. 1 EN' => '/en//pdf/' . $cavo->famiglia->codice_articolo . '.pdf',
                'ExternalDiameter / ND' => $caratteristicheCavo[1],
                'Connections / Number ' => $caratteristicheCavo[0],
                'Cable type' => $cavo->famiglia->nome,   
                'PreviewPicture' => '/immagini/' . $cavo->famiglia->immagine,
                'EDP catalogue structure level 1 EN' => 'Cavo',
            ];
        });
theblack68's avatar

Hey @aurawindsurfing

Maybe you can help me ;(

I have 3 tables in database >>> with a many to many relationship:

  • cables

  • feature

  • cable_feature

I have 2 Model (I have set a public function belongsToMany):

  • Cable

  • Feature

I have a controller for send data to View

In this view I have a table >>> I get some value from a controller (Cable name >>> cable family and so on).

My problem are the column of the feature >>> I need to have 4 feature:

  • Voltage

  • ColorType

  • Pvc

  • Shield

Not all the cables have all 4 features so I need to let Empty if the feature are not set ...you can give me an Help please?

Example:

[cable] [voltage] [ColorType] [Pvc] [Shield]

[cable12] [450 Volt] [red] [empty] [empty]

[cable13] [480 Volt] [empty] [Yes] [Yes]

Thanks a lot ;)

aurawindsurfing's avatar

Hey @theblack68

While defining your migrations set fields as ->nullable() this will allow them to be set to null if needed.

theblack68's avatar

HEY @aurawindsurfing

So when I create or update records in the table cable_feature even if they don't have that feature I have to set it to null?

aurawindsurfing's avatar

Hey @theblack68

No, if cable_feature is pivot table Many-to-Many then you just set the values you need. If the others are not set then they will not show in your results, which is what you want I think.

theblack68's avatar

Hey @aurawindsurfing

I have resolved so:

  • I get with in_array the only features that I want by their ID (5, 6, 7, 8)

  • With array_column I have set the Key with the ID of Feature

  • And finally I check the Key with the Isset ...so If isset I show the value else I show a "-"

...I don't know if is the right way but works good!

Thanks ;)

1 like

Please or to participate in this conversation.