Voyager: how to define one to many relationships ????

Created on 18 Nov 2016  路  7Comments  路  Source: the-control-group/voyager

enhancement help wanted

Most helpful comment

Another approach that I prefer is to create a Post model that extends Voyager's Post class:

<?php

namespace App;

class Post extends \TCG\Voyager\Models\Post

Then you can define all the relationships you want and not overwrite Voyager's functionality.

All 7 comments

Currently they're defined directly in the view- take a look at the Post example included. But this is fairly limited and only works in one direction.

188 Adds many to many and with a couple tweaks this could resolve this issue as well.

Another approach that I prefer is to create a Post model that extends Voyager's Post class:

<?php

namespace App;

class Post extends \TCG\Voyager\Models\Post

Then you can define all the relationships you want and not overwrite Voyager's functionality.

@brandonferens I think his question was more generic, meaning how do you define a relationship for any class and then display it as an option in the view.

Perhaps. The initial comment was fairly... vague. But what you are saying would be nice. By saying

display it as an option in the view

I am assuming you are talking about Voyager's admin area, and not some front facing page.

Ya, you're right- it's pretty hard to tell what he was asking from a body-less post.

And correct- meaning in the admin bread/edit-add view. Issue #188 actually already resolves this for n-n relationships so just need to apply this if the option is set for regular select boxes.

I haven't created a separate pull request for 1-n because I'm waiting on issue #200 to avoid cluttering this controller. Multiple selects can then extend the select field to stay DRY.

This is going to be implemented in the version 0.10 release :)

You can see how to create a belongsToMany or many-to-many relationship in the documentation here: https://the-control-group.github.io/voyager/docs/

Hope that helps. Let me know if you experience any problems implementing this. BTW, you can also do a one-to-one with the select dropdown 馃憤

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings