thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.8k stars 2.67k forks source link

No Belongs-To-Many relation to current model in BREAD #2193

Open myMartek opened 7 years ago

myMartek commented 7 years ago

Description:

I would like to create a "belongs-to-many" relation from article to article which is called "related articles".

When I try to do this I can choose a pivot table but the system automatically chooses the keys for the relation. In this case it tries to use twice article_id as keys which is obviously not possible.

The fix would be to allow users to customize the key names which should be used inside of the pivot.

Steps To Reproduce:

anbulanov commented 6 years ago

I also have the same problem. I'v found Link to GitHub article, but cant understand how to implement this in Voyager BREAD.

textoo commented 6 years ago

Hello,

found out a solution to this problem:

create a new model that extends your model, and specify the model table

namespace App;
use Illuminate\Database\Eloquent\Model;

class RelatedTest extends Test
{
 protected $table = 'tests';
}

so now you have a copy of the model that uses the same table but has a different name that can be matched in pivot table.

in this case you can create a pivot table named "test_test" with "test_id" and "related_test_id" columns

then in BREAD editor select 'tests' table as target with App\RelatedTest model and use test_test as pivot