Open lionskape opened 6 years ago
If you or someone sponsor this
@lionskape, any update on this, did you get a solution?
This would be great if it worked on Laravel Nova..
No.
@lazychaser, kindly requesting for a solution in regards to Laravel Nova.
@kasirye feel free to send a PR 😄
@kasirye what do you need?
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();
}
@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 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)
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();
}
Hello, it would be perfect, if you create Nova tool for your package.