lazychaser / laravel-nestedset

Effective tree structures in Laravel 4-8
3.63k stars 471 forks source link

Laravel Nova support #309

Open lionskape opened 6 years ago

lionskape commented 6 years ago

Hello, it would be perfect, if you create Nova tool for your package.

lazychaser commented 6 years ago

If you or someone sponsor this

kasirye commented 5 years ago

@lionskape, any update on this, did you get a solution?

This would be great if it worked on Laravel Nova..

lionskape commented 5 years ago

No.

kasirye commented 5 years ago

@lazychaser, kindly requesting for a solution in regards to Laravel Nova.

flangofas commented 5 years ago

@kasirye feel free to send a PR 😄

thoresuenert commented 4 years ago

@kasirye what do you need?

image It is working out of the box ;)

Trick to show the tree:

Text::make('Name')
                ->displayUsing(function($name, $resource){
                    return str_repeat('  ', $resource->depth) . $name;
                })
                ->asHtml()
                ->onlyOnIndex(),

To load the Resource List view with the right order:

/**
     * Build an "index" query for the given resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function indexQuery(NovaRequest $request, $query)
    {
        return $query->withDepth()->defaultOrder();
    }
cch504 commented 4 years ago

@lionskape I made it work using package https://github.com/novius/laravel-nova-order-nestedset-field and adding the following fields to the resource

Text::make('Name')
    ->displayUsing(function ($name, $resource) {
        return str_repeat('→ ', $resource->depth) . $name;
    })->asHtml()->onlyOnIndex(),

Select::make('Parent Model', 'parent_id')
    ->options(function () {
        return ParentModel
            ::where('id', '!=', $this->id)
            ->get()
            ->reduce(function ($options, $model) {
                $options[$model['id']] = $model['name'];
                return $options;
            }, []);
    })
    ->nullable()
    ->onlyOnForms(),

Then adding the following to the resource file

/**
 * Build an "index" query for the given resource.
 *
 * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
 * @param  \Illuminate\Database\Eloquent\Builder  $query
 * @return \Illuminate\Database\Eloquent\Builder
 */
public static function indexQuery(NovaRequest $request, $query)
{
    return $query->withDepth()->defaultOrder();
}
roarkmccolgan commented 4 years ago

@cch504 Thanks for the example however I don't understand why you would need the package you mentioned?

Would this not work without it?

Reason I ask is that package seems to be dead, and doesn't work on latest Nova (3.x)

saskaak commented 3 years ago

The solution by @thoresuenert works for me. However, using this resource as a BelongsToMany field on another resource creates an invalid SQL query. This might also apply to other types of relationship fields. I disabled this on relationship fields by replacing the indexQuery with:

public static function indexQuery(NovaRequest $request, $query)
{
    return $request->viaRelationship()
        ? $query
        : $query->withDepth()->defaultOrder();
}