Laravel nova 资源扩展/覆盖 create 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52389551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel nova resource extending/overriding the create method
提问by Wai Yan Hein
I am developing a web admin panel using Laravel Nova.
我正在使用Laravel Nova开发一个网络管理面板。
I am having an issue since Nova is quite a new technology.
我遇到了一个问题,因为 Nova 是一项相当新的技术。
What I would like to do now is I would like to add a hidden field or extend or override the create method.
我现在想做的是添加一个隐藏字段或扩展或覆盖 create 方法。
This is my scenario. Let's save I have a vacancy
nova resource with the following field.
这是我的场景。让我们保存我有一个vacancy
具有以下字段的nova 资源。
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Title')->sortable(),
Text::make('Salary')->sortable()
// I will have another field, called created_by
];
}
Very simple. What I like to do is I want to add a new field called created_by
into the database. Then that field will be auto filled with the current logged user id
($request->user()->id
).
很简单。我喜欢做的是我想created_by
在数据库中添加一个名为的新字段。然后该字段将自动填充当前登录的用户id
( $request->user()->id
)。
How can I override or extend the create function of Nova? How can I achieve it?
如何覆盖或扩展 Nova 的创建功能?我怎样才能实现它?
I can use resource event, but how can I retrieve the logged in user in the event?
我可以使用资源事件,但如何在事件中检索登录用户?
回答by Elena Kolevska
What you're looking for is Resource Events.
您正在寻找的是Resource Events。
From the docs:
从文档:
All Nova operations use the typical save, delete, forceDelete, restore Eloquent methods you are familiar with. Therefore, it is easy to listen for model events triggered by Nova and react to them. The easiest approach is to simply attach a model observer to a model:
所有 Nova 操作都使用您熟悉的典型的保存、删除、强制删除、恢复 Eloquent 方法。因此,很容易监听由 Nova 触发的模型事件并对其做出反应。最简单的方法是简单地将模型观察者附加到模型上:
If you don't feel like creating a new observable you could also create a boot
method in your eloquent model as so:
如果你不想创建一个新的 observable,你也可以boot
在你的 eloquent 模型中创建一个方法,如下所示:
public static function boot()
{
parent::boot();
static::creating(function ($vacancy) {
$vacancy->created_by = auth()->user()->id;
});
}
But please do note that these are a bit harder to track than observables, and you or a next developer in the future might be scratching their head, wondering how's the "created_at" property set.
但请注意,这些比 observable 更难跟踪,您或未来的下一个开发人员可能会挠头,想知道“created_at”属性集如何。
回答by Fawzan
In my opinion you should go for Observers
. Observers will make you code more readable and trackable.
在我看来你应该去Observers
。观察者将使您的代码更具可读性和可跟踪性。
Here is how you can achieve the same with Laravel Observers.
以下是使用 Laravel Observers 实现相同目标的方法。
AppServiceProver.php
AppServiceProver.php
public function boot()
{
Nova::serving(function () {
Post::observe(PostObserver::class);
});
}
PostObserver.php
PostObserver.php
public function creating(Post $post)
{
$post->created_by = Auth::user()->id;
}
OR
或者
You can simply hack a Nova
field with a meta.
你可以简单地Nova
用元来破解一个字段。
Text::make('created_by')->withMeta(['type' => 'hidden', 'value' => Auth::user()->id])
回答by ElectroBuddha
You could also do that directly within your Nova resource. Every Nova resource has newModel()
method which is called when resource loads fresh instance of your model from db. You can override it and put there your logic for setting any default values (you should always check if values already exist, and only set if they are null, which will only be the case when the model is being created for the first time, which is what you actually need):
您也可以直接在您的 Nova 资源中执行此操作。每个 Nova 资源都有一个newModel()
方法,当资源从 db 加载模型的新实例时会调用该方法。您可以覆盖它并将您的逻辑放在那里以设置任何默认值(您应该始终检查值是否已经存在,并且仅在它们为空时才设置,这只会在第一次创建模型时出现,即是你真正需要的):
public static function newModel()
{
$model = static::$model;
$instance = new $model;
if ($instance->created_by == null) {
$instance->created_by = auth()->user()->id;
}
return $instance;
}