whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
780 stars 229 forks source link

Flexible Content fields does not give an instance of Main Resource class #449

Open felixmensah321 opened 1 year ago

felixmensah321 commented 1 year ago

I have a Product resource. Within the resource I have this: Flexible::make('test', 'test') ->addLayout('', 'wysiwyg', [ Select::make('Type of Guide', 'type')->options($this->getDropDownData('GuideType', 'name'))->rules('required') ->fillUsing(function ($request, $model, $attribute, $requestAttribute) { dd($model); }) $model is not giving me the instance of the Product resource but an instance of Whitecube\NovaFlexibleContent\Layouts\Layout

Is there a way to get around this where I get the instance of the Product Resource?

riptin commented 1 year ago

Same question, but in custom layout.

wize-wiz commented 6 months ago

@felixmensah321 @riptin It shouldn't be a resource, but the model the resource uses.

wize-wiz commented 6 months ago

My commit is not going to work, so ignore that. It's not the model that's suppose to be passed when a field resides in a flex container. The problem here is that the Layout object needs to be passed because the field's attribute does not exist in the model, but in the given layout. Here the layout instance is always passed with $this.

It would be a great idea if the Layout instance had two methods, getResource() and getModel(), which again uses getResource() to retrieve the model.

// Layout::fill
public function fill(ScopedRequest $request, $model)
{
    return $this->fields->map(function ($field) use ($request, $model) {
        return $field->fill($request, $this); // $this == Layout instance.
    })
        ->filter(function ($callback) {
            return is_callable($callback);
        })
        ->values()
        ->all();
}

Through the request, we can always retrieve the resource or underlying model (please distinct the two)

So in your example

Flexible::make('test', 'test')
  ->addLayout('', 'wysiwyg', [
  Select::make('Type of Guide', 'type')
    ->options($this->getDropDownData('GuideType', 'name'))->rules('required')
    ->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
       dd($request->findResource()); // returns the current resource
       dd($request->findModel()); // returns the model of the current resource
     })