Skip to content

Self-Referential belongsToMany method on Model #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gn0rt0n opened this issue Apr 7, 2021 · 1 comment
Open

Self-Referential belongsToMany method on Model #16

gn0rt0n opened this issue Apr 7, 2021 · 1 comment

Comments

@gn0rt0n
Copy link

gn0rt0n commented Apr 7, 2021

public function fields(Request $request)
{
    return [
        NestedTreeAttachManyField::make('Offer Categories',"categories","App\Nova\Category"),
    ];
}

It looks like the second parameter should be the method on the categories class that defines the self-referential relationship. It requires a belongsToMany to be returned.

However, I don't see the specifics for defining that method in the https://github.com/lazychaser/laravel-nestedset documentation, or from your documentation.

Is this a method provided by the nestedset trait? If so, what is the name?

If not, could you provide an example of the relationship method that needs to be defined. i.e. \App\Models\Category->categories()

Thanks,
Gary

@phoenix-lib
Copy link
Owner

Hi, I think this example will help.

class Offer extends Model
{
    public function categories(): BelongsToMany
    {
         return $this->belongsToMany(Category::class);
    }
}
class Category extends Model
{
    use NodeTrait;
}
class Offer extends \App\Nova\Resource
{
     public static $model = \App\Models\Offer::class;

     public function fields(Request $request)
     {
         return [
            NestedTreeAttachManyField::make(__('Offer Categories'), 'categories', 'App\Nova\Category'),
         ];
     }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants