Laravel - Additional relationship on a pivot table


I have a regular pivot table with 2 keys. However, I also have a 3rd column where I want to store a different key with a one to many relationship. Is this possible to have?


Pivot table:<br /> Organization 1 | Organization 2 | Relation type<br /> 1 | 2 | 1<br /> 1 | 3 | 2

In this case organization number 1 has a relation with organization number 2 with the relation type being number 1. Organization number 1 also has a relation with organization number 3 with relation type 2.

Now is my question, how do I set up that additional one to many relationship on the pivot table?


What you have here is a ternary relationship. You are saying that an organisation A relates with an organisation B and a relationship type. This is a very uncommon use case because in the vast majority of cases ternary relationships can be simplified to binary ones. You need a very deep inspection of your data model to determine whether your case can be simplified, but assuming that it can't here's my suggestions.

It's worth checking <a href="https://laravel.com/docs/5.5/eloquent-relationships#many-to-many" rel="nofollow">the eloquent docs</a> in particular under <em>Defining Custom Intermediate Table Models</em> for this. Note that this requires Laravel 5.4+ to work.

The following should work:

class OrganisationOrganisationLink extends Pivot { public relationType() { return $this->belongsTo(RelationType::class); //You need to specify the foreign key correctly as a 2nd parameter } }

Then in your original model:

class Organisation extends Model { public relatedOrganisation() { return $this->belongsToMany(self::class)->using(OrganisationOrganisationLink::class); } }

Then when making practical use of this you can e.g. do:

$organisation = Organisation::with('relatedOrganisation')->first(); echo "Got ".$organisation->name." which relates to " .$organisation->relatedOrganisation->first()->name ." with relationship type " $organisation->relatedOrganisation->first()->pivot->relationshipType()->value('name');

Of course the fields I've assumed may not exist but hopefully you get the idea.


  • Multi-dimensional Array - Check for diagonal consecutive values
  • Update text box, but a letter remain doesn't get deleted react js
  • Translating C to MIPS with a ternary operator
  • java: weird NullPointerException in ternary operator (? : )
  • How do I imply the type of the value when there are no type parameters or ascriptions?
  • How to conditionally add widgets to a list?
  • Fetch all binded events using jquery
  • Text box text capture using Jquery always 'one character behind'
  • Extracting multiple substrings that come after certain characters in a string using stringi in R
  • Several images on one UIButton
  • Jenkins and SSL certificates
  • In Rails, is it possible to use the show view as an edit view?
  • Drawing multiple triangles in OpenGL
  • Google Maps: Norwegian postcodes not returning any results
  • How to use Boost Spirit with variant with more than 20 types?
  • combination of smote and undersampling on weka
  • How can I preserve a pandas multi-index between a to_excel() and a read_excel()?
  • Does Xcode 6 Simulator use x86_64?
  • Using raw SQL in django-rest framework?
  • Condition in ternary operator doesn't cause any change
  • Accessing the main thread in a TKinter script?
  • What does this PHP (function/construct?) do, and where can I find more documentation on it?
  • SQL complicated recursive CTE
  • finding greatest prime factor using recursion in c
  • How can I determine if process is 32 or 64Bit from a handle?
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • crash in __tcf_0
  • ckeditor and jquery UI dialog not working
  • Shouldn't else be indented in the below code
  • Most efficient way to move table rows from one table to another
  • For loop with if condition on multiple R functions
  • How does document.ready work with angular element directives?
  • Content-Length header not returned from Pylons response
  • Cancel a live stream “fast motion” catch-up in Flash
  • Lost migrations and Azure database is now out of sync
  • PHP: When would you need the self:: keyword?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory