Laravel Nova Self-referential Relationship
Asked Answered
Y

3

7

In Laravel, if I want to create a self-referential relationship I can do the following:

class Post extends Eloquent
{
    public function parent()
    {
        return $this->belongsTo('Post', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('Post', 'parent_id');
    }
}

How can I make a Laravel Nova resource display this connection?

public function fields(Request $request)
{
    return [
        Text::make('Autor', 'author'),
        Select::make('Type', 'type')->options([
            'News' => 'news',
            'Update' => 'update',
        ]),
        BelongsToMany::make('Post') // does not work
    ];
}
Yulma answered 3/12, 2018 at 14:15 Comment(0)
D
11

You can achieve what you want like this:

BelongsTo::make('Parent', 'parent', \App\Nova\Post::class),

HasMany::make('Children', 'children', \App\Nova\Post::class),

This will allow to choose a parent post when you create or update a post. When you are in the detail page of a post, you can see all its children.

public function fields(Request $request)
{
    return [
        Text::make('Author', 'author'),
        Select::make('Type','type')->options([
            'News' => 'news',
            'Update' =>  'update',
        ]),
        BelongsTo::make('Parent', 'parent', \App\Nova\Post::class),
        HasMany::make('Children', 'children', \App\Nova\Post::class),
    ];
}

Note: Please note that the third param to BelongsTo::make() and HasMany::make() is a reference to the Post Resource, not Post model.

Determiner answered 3/12, 2018 at 16:29 Comment(0)
M
3

There is another situation, where you will find same issue, if you have parent column name parent and also relationship parent like

$table->bigIncrements('id');
$table->string('category');
$table->unsignedBigInteger('parent')->nullable();

and In model

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

It will be unable to recognize the parent property and you will face this problem again, in that case, you can change the relationship name or column name, and it will work fine.

Also remember the arguments for Nova BelongsTo relationship

Argument 1. Name to display (e.g. Parent)

Argument 2. Name of the relationship as used in the model (e.g. parent)

Argument 3. The Nova Resource (e.g. App\Nova\Category)

Mneme answered 14/6, 2019 at 6:20 Comment(4)
Why should you name a foreign key not with a _id postfix?Xanthic
@Xanthic It seems you have a different question, could you please post it as a different question with more details?Mneme
Its more a rhetorical question. Your answer is fixing a problem, which the question creator didn't have and normaly you shouldn't have at all, since its not a good practice to name your foreign key like this.Xanthic
@Xanthic I don’t remember the reason behind this naming, but yes the naming conventions should be correct.Mneme
S
0

make sure to have title in your resource that represent the display name

class Post extends Resource
{
       public static $title = 'name';
}
Stargell answered 6/11, 2023 at 16:22 Comment(1)
this is not valid class.Airplane

© 2022 - 2025 — McMap. All rights reserved.