> For the complete documentation index, see [llms.txt](https://helpdocs.quickadminpanel.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://helpdocs.quickadminpanel.com/create-panel/relationship-fields-belongsto-belongstomany.md).

# Relationships fields: belongsTo/belongsToMany

QuickAdminPanel supports two types of relationships between CRUDs:&#x20;

* **One-to-Many:** belongsTo() + hasMany()
* **Many-to-Many:** belongsToMany().

To create a relationship, you just need to choose field type **belongsTo** or **belongsToMany.**

![](https://3430351544-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7hdtfDr4t_C1ptQgyo%2F-M7q9qQ1dRwIBsJiw4iO%2F-M7qHF8JNFo4Lstl8bCg%2FScreen%20Shot%202020-05-21%20at%2011.10.03%20AM.png?alt=media\&token=8e92cd63-cf7d-40b5-b203-512c1ddde489)

You don't need to manually create separate field or pivot table, they will be created automatically:

* In case of **belongsTo**, QuickAdminPanel will automatically create DB column **xxxxxx\_id** with a foreign key to the parent table.
* In case of **belongsToMany**, QuickAdminPanel will automatically create a **pivot table**, you don't need to create that table manually.

Here are examples of filled-in forms for belongsTo and belongsToMany:

![This will automatically generate column vehicles.owner\_id with foreign key to users table](https://3430351544-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7hdtfDr4t_C1ptQgyo%2F-M7q9qQ1dRwIBsJiw4iO%2F-M7qHcL8Cw_WlsFTLJbc%2FScreen%20Shot%202020-05-21%20at%2011.10.36%20AM.png?alt=media\&token=13326ffb-713f-4e4b-8a05-f21c80153015)

![This will automatically generate role\_user pivot table](https://3430351544-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7hdtfDr4t_C1ptQgyo%2F-M7q9qQ1dRwIBsJiw4iO%2F-M7qHuaLlRSIeNA9cuk_%2FScreen%20Shot%202020-05-21%20at%2011.13.37%20AM.png?alt=media\&token=6d885cd3-db02-4a0b-a0d3-a6f263c0941c)

## Showing Related Children Records on Parent's Page

One more feature: you can enable the children's records to be shown in a **show()** method page of a parent CRUD.

![](https://3430351544-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7hdtfDr4t_C1ptQgyo%2F-MAArvKteXQHoiOWCOD5%2F-MAAsCwwKKJWlDhWVwMn%2FScreen%20Shot%202020-06-19%20at%2012.18.15%20PM.png?alt=media\&token=7ce41f3b-0c31-4a3f-947c-594419695d7f)

If you tick that checkbox, then on the View page of the Parent's CRUD you will see a table of children's record:

![You click "View" on parent's record](https://3430351544-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7hdtfDr4t_C1ptQgyo%2F-MAArvKteXQHoiOWCOD5%2F-MAAuiZTHu8Z71PodWow%2FScreen%20Shot%202020-06-19%20at%2012.29.47%20PM.png?alt=media\&token=eb9455ce-5a20-478f-9f7b-7ca70ea6bbe5)

![And it will show children records at the bottom](https://3430351544-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7hdtfDr4t_C1ptQgyo%2F-MAArvKteXQHoiOWCOD5%2F-MAAupLAYRTfW9Io_1zZ%2FScreen%20Shot%202020-06-19%20at%2012.30.31%20PM.png?alt=media\&token=49ba6519-d248-4653-9e5c-29a6539a8d67)

## Video Demo of Relationships

This is a quick video demo of both belongsTo and belongsToMany.

{% embed url="<https://www.youtube.com/watch?v=Txjzcz19d0s>" %}

## BelongsTo Relationship to Itself: The Same CRUD

Quick video demo how you can create a relationship to itself, for parent-children "tree". For example, Category belongsTo Category with category\_id field.

{% embed url="<https://www.youtube.com/watch?v=kn-0yZU5mZ8>" %}

## Customizations of Relationships&#x20;

Articles that may also be helpful:

* [Master-Detail Form in Laravel + jQuery: Create Order With Products](https://quickadminpanel.com/blog/master-detail-form-in-laravel-jquery-create-order-with-products/)
* [Laravel BelongsTo and BelongsToMany with Same Table: Possible? Worth it?](https://quickadminpanel.com/blog/belongsto-and-belongstomany-with-same-table-possible-worth-it/)
* [One-To-Many with Soft-Deletes. Deleting Parent: Restrict or Cascade?](https://quickadminpanel.com/blog/one-to-many-with-soft-deletes-deleting-parent-restrict-or-cascade/)
* [Customization: How to Make Two Dependent Dropdowns, like Parent-Child?](https://helpdocs.quickadminpanel.com/customizing-the-code/dependent-dropdowns-parent-child)
* [Laravel BelongsToMany: Add Extra Fields to Pivot Table](https://quickadminpanel.com/blog/laravel-belongstomany-add-extra-fields-to-pivot-table/)
