laravel-nestedset icon indicating copy to clipboard operation
laravel-nestedset copied to clipboard

Laravel Nova support

Open lionskape opened this issue 7 years ago • 9 comments

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

lionskape avatar Sep 09 '18 18:09 lionskape

If you or someone sponsor this

lazychaser avatar Sep 10 '18 05:09 lazychaser

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

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

kasirye avatar Apr 12 '19 13:04 kasirye

No.

lionskape avatar Apr 12 '19 14:04 lionskape

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

kasirye avatar Apr 23 '19 13:04 kasirye

@kasirye feel free to send a PR 😄

flangofas avatar Apr 24 '19 07:04 flangofas

@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();
    }

thoresuenert avatar Feb 23 '20 10:02 thoresuenert

@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();
}

cch504 avatar Apr 29 '20 06:04 cch504

@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)

roarkmccolgan avatar May 25 '20 13:05 roarkmccolgan

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();
}

saskaak avatar Jul 08 '21 09:07 saskaak