添加列到 Laravel Nova 资源

huangapple go评论54阅读模式
英文:

How to add a column to Laravel Nova resource

问题

我正在尝试向Laravel Nova资源添加自定义列。

在字段中,可以像这样添加存在于数据库中的列:

public function fields(NovaRequest $request) {
     ID::make()->sortable(),
     Text::make('Country')->sortable();
}

但是,我想添加一个在数据库表中不存在的自定义列。它的值可以通过使用Eloquent获得:

$count = Hostel::where('country_id', $country_id)
            ->distinct('type_id')
            ->whereMonth('created_at', Carbon::now()->month)
            ->count();

其中$country_id是资源中的ID。

我该如何实现这一点?并确保在使用“添加资源”时,该字段不会显示?谢谢!

英文:

I'm trying to add a custom column to the Laravel Nova resource.

In the fields the columns that exist in database can be added like:

public function fields(NovaRequest $request) {
     ID::make()->sortable(),
     Text::make('Country')->sortable();
}

But, I wanted to add a custom column that doesn't exist in that database table. Its value can be obtained by using Eloquent:

$count = Hostel::where('country_id', $country_id)
            ->distinct('type_id')
            ->whereMonth('created_at', Carbon::now()->month )
            ->count();

where $country_id is the ID in the resouce.

How would I acheive this? and making sure this field doesn't appear when using Add Resource? Thank you!

答案1

得分: 2

I do not know if you are using Nova 3 or 4, but the way of doing it is the same. I do not have the Nova package, so I am 100% using the documentation, I may be missing something in my code, but it should perfectly work.

You need to use a computed field. It will let you set what type of field it is going to be, but you must set the value of it, exactly what you need. In this case, I will use a Number field, but feel free to use Text or whatever you would like to:

use Laravel\Nova\Fields\Number;

public function fields(NovaRequest $request)
{
    return [

        ID::make()
            ->sortable(),

        Text::make('Country')
            ->sortable(),

        Number::make('Amount of Hostels in Country', function () {
            return Hostel::where('country_id', $this->country_id)
                ->distinct('type_id')
                ->whereMonth('created_at', now()->month)
                ->count();
        })
            ->hideWhenCreating()
            ->hideWhenUpdating(),
    ];
}

I added hideWhenCreating and hideWhenUpdating, because you said you don't want it when adding a resource, but I also added hidden on update because it would not make much sense to see it when updating a resource. This field should be read-only. In that case, read the same page I shared before (computed), it has all the methods you need to hide them, make them read-only, etc.

Maybe, instead of using hideWhenXXXXX, you may be able to use exceptOnForms, but I do not know what hideWhenXXXX is running in the background, check the method and see if it helps you.

英文:

I do not know if you are using Nova 3 or 4, but the way of doing it is the same. I do not have the Nova package, so I am 100% using the documentation, I may be missing something in my code, but it should perfectly work.

You need to use a computed field. It will let you set what type of field it is going to be, but you must set the value of it, exactly what you need. In this case, I will use a Number field, but feel free to use Text or whatever you would like to:

use Laravel\Nova\Fields\Number;

public function fields(NovaRequest $request)
{
    return [

        ID::make()
            ->sortable(),

        Text::make('Country')
            ->sortable(),

        Number::make('Amount of Hostels in Country', function () {
            return Hostel::where('country_id', $this->country_id)
                ->distinct('type_id')
                ->whereMonth('created_at', now()->month)
                ->count();
        })
            ->hideWhenCreating()
            ->hideWhenUpdating(),
    ];
}

I added hideWhenCreating and hideWhenUpdating, because you said you don't want it when adding a resource, but I also added hidden on update because it would make not much sense to see it when updating a resource. This field should be read only. In that case read the same page I shared before (computed), it has all the methods you need to hide them, make them read-only, etc.

Maybe, instead of using hideWhenXXXXX, you may be able to use exceptOnForms, but I do not know what hideWhenXXXX is running in teh background, check the method and see if it helps you.

huangapple
  • 本文由 发表于 2023年5月25日 22:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定