Foued MOUSSI
Foued MOUSSI

Reputation: 4813

Laravel Nova v2.8.0 : Customize action label

Is there an easy way to customize action label to my resource overview ? enter image description here

enter image description here

Product resource

//App\Nova\Product.php
namespace App\Nova;
use Illuminate\Http\Request;
use App\Nova\Actions\UploadProdcuts as UploadProdcuts;

class Product extends Resource
{
    //...
    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [
            new UploadProdcuts
        ];
    }
}

Upload Products Action

//App\Nova\Actions\UploadProdcuts.php
namespace App\Nova\Actions;

use Illuminate\Bus\Queueable;
use Laravel\Nova\Actions\Action;
use Illuminate\Support\Collection;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Nova\Fields\File;
use App\Imports\ProductsImport;
use Maatwebsite\Excel\Facades\Excel;

class UploadProdcuts extends Action
{
    use InteractsWithQueue, Queueable, SerializesModels;

    //public $onlyOnDetail = true;
    //public $onlyOnIndex = true;

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        Excel::import(new ProductsImport, request()->file('file'));

        return Action::message('Products Uploaded Successfully!');
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [
            File::make('File')->rules('required', 'max:50000', 'mimetypes:application/csv,application/excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
        ];
    }
}

Upvotes: 2

Views: 1805

Answers (1)

Chin Leung
Chin Leung

Reputation: 14941

You can either set a $name property in your class, or add the function. If you take a look at the name function from Nova's Action class (vendor/laravel/nova/src/Actions/Action.php):

/**
 * Get the displayable name of the action.
 *
 * @return string
 */
public function name()
{
    return $this->name ?: Nova::humanize($this);
}

So you could set a property in your class like this:

class UploadProdcuts extends Action
{
    public $name = 'My Action';
}

Or simply add the name function:

/**
 * Get the displayable name of the action.
 *
 * @return string
 */
public function name(): string
{
    return __('My Action Name');
}

Side note, you have a typo in your class name. You've named it UploadProdcuts instead of UploadProducts.

Upvotes: 2

Related Questions