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

dadub's avatar
Level 1

Display records from 2 tables through a pivot table

Hello,

I have two tables :

Herbs :

1 herb1 2 herb2

herb_forms

1 Tea 2 Caps

One herb can have several forms and one forms have several herbs, I created a pivot table :

herbs_have_forms

1 - 1 1- 2 2- 1

I would like to know how can I display the name of herbs and his form please ?

I did all, seed, model, controller... but I don't know how can I create a many to many query ?

Thank you in advance for your help.

0 likes
7 replies
MichalOravec's avatar

You have to set your relationships

https://laravel.com/docs/7.x/eloquent-relationships#many-to-many

But be careful, your pivot table herbs_have_forms is not as default laravel convention.

Then you can write your code like this

$herb = Herb::with('forms')->first();

$herb->forms; // this will be your collection of forms

an in opposite way

$form = HerbForm::with('herbs')->first();

$form->herbs; // this will be your collection of herbs

I hope you got an idea.

dadub's avatar
Level 1

Hello Michal,

Thank you for your reply.

I tried your code, I have this message :

Call to undefined relationship [forms] on model [App\Herb].

My herb model contains :

public function herb_forms() { return $this->belongsToMany(HerbForm::class); }

and my herb_forms contains :

public function herbs() { return $this->belongsToMany(Herb::class); } my model herbs_has_form is empty, like this :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class HerbHasForm extends Model { // }

Once I will understand for this one, I will understand for all others.

Thank you again for your help.

David

MichalOravec's avatar

Naming of those method it was just example.

Ok you don't need HerbHasForm model, you have to set belongsToMany for your custom table.

Herb model

public function herb_forms() { 
    return $this->belongsToMany(HerbForm::class, 'herbs_has_form'); 
}

HerbForm model

public function herbs() { 
    return $this->belongsToMany(Herb::class, 'herbs_has_form'); 
}

From docs

Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method

In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to

I hope you got it and set it correctly :) So if your foreign keys are different, you have to set third and fourth argmunent of $this->belongsToMany in both models.

dadub's avatar
Level 1

Great reply Michal, THANK YOU very much.

With this code

$herb = Herb::with('herb_forms')->get();

dd($herb); // this will be your collection of forms*/

I have this collection :

#items: array:10 [▼ 0 => App\Herb {#522 ▼ #connection: "mysql" #table: "herbs" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:6 [▼ "id" => 1 "name" => "Echinacées" "sciname" => "Echinacea sp." "created_at" => "2020-05-16 20:26:48" "updated_at" => "2020-05-16 20:26:48" "user_id" => 1 ] #original: array:6 [▶] #changes: [] #casts: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:1 [▼ "herb_forms" => Illuminate\Database\Eloquent\Collection {#538 ▼ #items: array:1 [▼ 0 => App\HerbForm {#573 ▼ #connection: "mysql" #table: "herb_forms" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:4 [▼ "id" => 1 "name" => "Caps" "created_at" => "2020-05-16 20:26:48" "updated_at" => "2020-05-16 20:26:48" ] #original: array:6 [▶] #changes: [] #casts: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:1 [▶] #touches: [] +timestamps: true #hidden: [] #visible: [] #fillable: [] #guarded: array:1 [▶] } ] } ] #touches: [] +timestamps: true #hidden: [] #visible: [] #fillable: [] #guarded: array:1 [▶] }

If I try to display herb_forms with this code :

dd($herb->forms); // this will be your collection of forms*/

I have this error :

Property [herb_forms] does not exist on this collection instance.

How can I please reach the herb_forms value ?

Thank you again for all.

David

MichalOravec's avatar

Instead of ->get() use ->first() to get only one model, and not collections of your model

$herb = Herb::with('herb_forms')->first();

$herb->herb_forms;

With collection

$herbs = Herb::with('herb_forms')->get();

foreach ($herbs as $herb) {
    $herb->herb_forms;
}
1 like
dadub's avatar
Level 1

Thank you so much Michal for all you are doing for everybody here...

I will read again your reply in a few hours and I will to understand.

All is working great tahnks to you.

Thank you again.

DAvid

dadub's avatar
Level 1

Hi Michal,

I try to use Laravel with all conventions... and I'm a little bit worry about what you said on yesterday :

But be careful, your pivot table herbs_have_forms is not as default laravel convention.

How have I to do to be OK with Laravel ?

Thank you for your help.

David

Please or to participate in this conversation.