Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control over created models/attached fields #2506

Closed
genesiscz opened this issue Apr 13, 2020 · 7 comments
Closed

Control over created models/attached fields #2506

genesiscz opened this issue Apr 13, 2020 · 7 comments

Comments

@genesiscz
Copy link

So actually I am reacting to this PR https://github.com/laravel/nova/pull/869 where we got the hidden fields, which is great.

Now, it may be a question if there's already a way to do this, or a feature request if it doesn't.

I have several resources that needs additional model fields to be filled.
This is quite easy, just overriding the newModel to fill the required fields which aren't directly controllable by the form.

    static function newModel() {
        /** @var VoucherModel $model */
        $model = parent::newModel();
        $model->creator_id = Auth::id();
        return $model;
    }

Now the problem rises when I don't actually know the field value and actually need to derive the value from the request. This might be a bad example, but let's say I add a weight to the ingredient resource. The products resource, that uses this ingredient, needs to update it's total weight based on all of their ingredients.

   static function newModel($request) {
      if ($request->isUpdate()) {
        $weightDiff = $request->oldModel->weight - $request->weight;
        $request->model->products()->...->addWeight($weightDiff);
      } else {
         ...
      }
   }

Also, I have several BelongsToMany fields, which, on attach, needs to add additional data about the attached (pivot) row.

  static function onAttach($resource) {
     if ($resource->name == "ingredient") {
        $resource->attaching(function($model, $request) {
            $model->creator_id = Auth::id();
        });
     }
  }

Does this make sense? We don't actually have an (easy) way to modify the fields that are being inserted into the pivot tables.

@socieboy
Copy link

You can use Observers.
Observe for the model and use the methods, updating, creating.

@genesiscz
Copy link
Author

@davidhemphill
Copy link
Contributor

Does using the fillUsing callback function on your pivot fields not work for your use-case?

public function fields() {
    return [
        BelongsToMany('Example')->fields([
            Text::make('Weight')->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
                // Fill pivot fields here                
            })
        ]),
    ];
}

@genesiscz
Copy link
Author

genesiscz commented Apr 19, 2020

It kinda is, but it isn't documented at all. On a sidenote, I haven't found the new Hidden field documented either.

This method would be ok for using the model/request to derive the data, but what if I want to fill more fields that are not part of this, eg. user_id. Let's say it's not a field I can show to the user because it's a secret, would I just go with Hidden field filling the value somehow to empty so the value doesn't show up for user in code and fillUsing to edit the model?


Another question is, if I have first field which gets changed by fillUsing by $model->field = $anotherValue, and another field which needs the old value of the first field, my only hope is to exchange the positioning of these two and probably can cause (in rare ocassions) weird bugs. Am I right?

It is possible that my thinking is maybe to deep and that these situations don't happen, just a thought.

@stale
Copy link

stale bot commented Jun 23, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 23, 2020
@genesiscz
Copy link
Author

It still is kinda issue, I had to go with third party package which doesn't seem to work well with Nova sometimes. Pivot data observers are still a problem.

@stale stale bot removed the stale label Jun 25, 2020
@davidhemphill
Copy link
Contributor

Yes you can grab the current NovaRequest from IoC: app(NovaRequest::class).

Nova's resources and fields are not designed to manage pivot data. Any effort to do so is a hack outside of what's officially supported at this time. If using Observers or fillUsing callbacks on your fields isn't enough to satisfy your requirements, I don't think we have any other options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants