Laravel nova resource extending/overriding the create method

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 18:30:50

问题


I am developing a web admin panel using Laravel Nova.

I am having an issue since Nova is quite a new technology.

What I would like to do now is I would like to add a hidden field or extend or override the create method.

This is my scenario. Let's save I have a vacancy nova resource with the following field.

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).

How can I override or extend the create function of Nova? How can I achieve it?

I can use resource event, but how can I retrieve the logged in user in the event?


回答1:


What you're looking for is 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:

If you don't feel like creating a new observable you could also create a boot method in your eloquent model as so:

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.




回答2:


In my opinion you should go for Observers. Observers will make you code more readable and trackable.

Here is how you can achieve the same with Laravel Observers.

AppServiceProver.php

public function boot()
    {
        Nova::serving(function () {

            Post::observe(PostObserver::class);

        });
    }

PostObserver.php

public function creating(Post $post)
    {

        $post->created_by = Auth::user()->id;

    }

OR

You can simply hack a Nova field with a meta.

Text::make('created_by')->withMeta(['type' => 'hidden', 'value' => Auth::user()->id])



回答3:


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):

public static function newModel()
{
    $model = static::$model;
    $instance = new $model;

    if ($instance->created_by == null) {
        $instance->created_by = auth()->user()->id;
    }

    return $instance;
}


来源:https://stackoverflow.com/questions/52389551/laravel-nova-resource-extending-overriding-the-create-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!