nova-package-development icon indicating copy to clipboard operation
nova-package-development copied to clipboard

Repeatable fields

Open cerelli opened this issue 8 years ago • 2 comments

Hello, I would like to create a package similar to this one, https://github.com/fourstacks/nova-repeatable-fields, but that goes to save the "repetable" fields in another table. Every time I save, it gives me an error saying that the attribute of the resource does not exist. How can I create a field that is not a resource attribute?

cerelli avatar Sep 06 '18 14:09 cerelli

@cerelli Can you give us more details and the code you have currently?

You're going to need to modify or create your own field, nova-repeatable-fields won't do what you want.

robmpreston avatar Sep 06 '18 18:09 robmpreston

I modify the nova-repeatable-fields. This field return a json in fillAttributeFromRequest. I have a lead resource and in in lead model:

    public function notes()
    {
        return $this->hasMany('App\Note');
    }

This is my test in fillAttributeFromRequest of repeator.

    protected function fillAttributeFromRequest(NovaRequest $request,
                                                $requestAttribute,
                                                $model,
                                                $attribute)
    {
        $class = get_class($model);
        $arr = json_decode($request[$requestAttribute], true);
        $notes = new \App\Note;
        $notes->fill($arr);
        dd($class, $requestAttribute, $arr, $notes);
        if ($request->exists($requestAttribute)) {
            $notes = [
                    ['title' => 'Titolo 1','user_id' => 1,'priority' => 'Medium','body'=>'aa'],
                    ['title' => 'Titolo 2','user_id' => 1,'priority' => 'Low','body'=>'bbbbb']
                ];
            $model->$requestAttribute()->createMany($request[$requestAttribute]);
            // $model->{$attribute} = json_decode($request[$requestAttribute]);
            // dump($model->{$attribute} );
        }
    }

How can i save hasMany relation ? json_decode($request[$requestAttribute], true);

cerelli avatar Sep 06 '18 20:09 cerelli