Search code examples
laravellaravel-nova

Laravel Nova - Change Tool to Resource Tool


I'm trying to convert a build Nova tool to a resource tool. I've tried to change my Tool to extend ResourceTool instead of Tool and add the resource tool to the Resource in the fields, but it's not showing up. I also changed the code of ToolServiceProvider to match that of a ResourceTool but it does not work unfortunately.

I've searched the internet but I seem to be the only one having this issue, anyone ever experienced this and know what to do? I'm not getting any error message, the resource tool just is not showing up.

Here is my code, I removed some of it for readability and confidentiality.

Product (Resource)

<?php

namespace App\Nova;

use confidentiality\SalesHistory\SalesHistory;

class Product extends Resource
{
    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            SalesHistory::make(),

        ];
    }
}

SalesHistory (Resource tool)

<?php

namespace confidentiality\SalesHistory;

use Laravel\Nova\ResourceTool;

class SalesHistory extends ResourceTool
{
    /**
     * Get the displayable name of the resource tool.
     *
     * @return string
     */
    public function name()
    {
        return 'Sales History';
    }

    /**
     * Get the component name for the resource tool.
     *
     * @return string
     */
    public function component()
    {
        return 'sales-history';
    }
}

ToolServiceProvider (Resource Tool)

<?php

namespace confidentiality\SalesHistory;

use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class ToolServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app->booted(function () {
            $this->routes();
        });

        Nova::serving(function (ServingNova $event) {
            Nova::script('sales-history', __DIR__.'/../dist/js/tool.js');
            Nova::style('sales-history', __DIR__.'/../dist/css/tool.css');
        });
    }

    /**
     * Register the tool's routes.
     *
     * @return void
     */
    protected function routes()
    {
        if ($this->app->routesAreCached()) {
            return;
        }

        Route::middleware(['nova'])
                ->prefix('nova-vendor/sales-history')
                ->group(__DIR__.'/../routes/api.php');
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Solution

  • I managed to fix it finally. I also had to change the tool.js file to match that of a Resource Tool.

    Nova.booting((Vue, router, store) => {
        Vue.component('sales-history', require('./components/Tool'))
    })