phoenix-lib / nova-nested-tree-attach-many

MIT License
35 stars 14 forks source link

Self-Referential belongsToMany method on Model #16

Open gn0rt0n opened 3 years ago

gn0rt0n commented 3 years ago
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 commented 3 years ago

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'),
         ];
     }
}