拉拉维尔新星 v2.8.0 : 自定义动作标签

有没有一种简单的方法来自定义我的资源概述的操作标签?

http://img4.mukewang.com/631ebf1e000160c422950991.jpg

http://img2.mukewang.com/631ebf240001a49716180656.jpg

产品资源


//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

        ];

    }

}

上传产品操作


//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'),

        ];

    }

}


慕森王
浏览 69回答 1
1回答

catspeake

可以在类中设置属性,也可以添加函数。如果您查看Nova的操作类(供应商/拉拉维尔/新星/src/操作/操作.php)中的函数:$namename/** * Get the displayable name of the action. * * @return string */public function name(){    return $this->name ?: Nova::humanize($this);}因此,您可以在类中设置一个属性,如下所示:class UploadProdcuts extends Action{    public $name = 'My Action';}或者只需添加以下函数:name/** * Get the displayable name of the action. * * @return string */public function name(): string{    return __('My Action Name');}旁注,您的类名中有一个拼写错误。您已将其命名为 。UploadProdcutsUploadProducts
打开App,查看更多内容
随时随地看视频慕课网APP