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

webdevb's avatar

Get all parents

I have a menu setup using https://github.com/harimayco/wmenu-builder and it's working quite well until it comes to trying to display the permalinks by getting the menu items parent but for it to keep going to get all parents.

Here is an example of the menu:

+-----+---------------------------------------------------------------------------------+--------------------------------------------------------------------------------+--------+
| id  | label                                                                           | link                                                                           | parent |
+-----+---------------------------------------------------------------------------------+--------------------------------------------------------------------------------+--------+
|   1 | Home                                                                            | homepage                                                                       |      0 |
|   2 | About Us                                                                        | about-us                                                                       |      0 |
|   3 | Services for you                                                                | services-for-you                                                               |      0 |
|   5 | Education                                                                       | education                                                                      |      0 |
|   6 | Services for business                                                           | services-for-business                                                          |      0 |
|   7 | Our People                                                                      | our-people                                                                     |      0 |
|   8 | Contact us                                                                      | contact-us                                                                     |      0 |
|   9 | Careers                                                                         | careers                                                                        |      0 |
|  10 | Press and Events                                                                | press-and-events                                                               |      0 |
|  27 | Care Home Funding                                                               | care-home-funding                                                              |      3 |
|  28 | Contentious Probate                                                             | contentious-probate                                                            |      3 |
|  29 | Financial Provisions                                                            | financial-provisions                                                           |     28 |
|  30 | Negligence Claims                                                               | negligence-claims                                                              |     28 |
|  31 | Proprietary Estoppel                                                            | proprietary-estoppel                                                           |     28 |
|  32 | Rectification Claims                                                            | rectification-claims                                                           |     28 |
|  33 | Trust Disputes                                                                  | trust-disputes                                                                 |     28 |
|  34 | Validity Claims                                                                 | validity-claims                                                                |     28 |
|  35 | Court of Protection                                                             | court-of-protection                                                            |      3 |
|  36 | Deputyship Applications                                                         | deputyship-applications                                                        |     35 |
|  37 | Financial Abuse                                                                 | financial-abuse                                                                |     35 |
|  38 | Gifts                                                                           | gifts                                                                          |     35 |
|  39 | Professional Deputyships                                                        | professional-deputyships                                                       |     35 |
|  40 | Purchasing and Adapting a Property                                              | purchasing-and-adapting-a-property                                             |     35 |
|  41 | Statutory Wills                                                                 | statutory-wills                                                                |     35 |
|  42 | Support for Solicitors                                                          | support-for-solicitors                                                         |     35 |
|  43 | Criminal Law                                                                    | criminal-law                                                                   |      3 |

So for instance, I want to get the permalink structure of Financial Provisions this has a parent id of 3 so the final permalink structure would be:

/services-for-you/contentious-probate/financial-provisions I can't for the live of me workout the best way of doing this, do I just create a relationship of it's self?

Can any help or shed any light on this.

0 likes
1 reply
burlresearch's avatar

For sure that's a good way to do it. There is nothing wrong with creating a relation from a table back to itself - in fact this is a great way to implement data-structures in your database. For your case, I'd expect something like:

    public function parent()
    {
        return $this->belongsTo(MenuItem::class, 'parent');
    }

As a note - I'd probably recommend making the column ->unsigned()->nullable() in your migration. This way you don't have to use a 0-id to represent a root node, but a null-parent would be a little more semantic. Since 0-id record doesn't actually exist in your database.

Along the same lines, you could extend this concept and build additional tree-operations into your model, if you think you'll need them:

    public function ancestors();    // or parents()...
    public function children(); // hasMany()
    // ... siblings?

Please or to participate in this conversation.