Laravel-admin: Embeds inside hasMany ignored when getting or storing

Created on 13 Jun 2017  ·  4Comments  ·  Source: z-song/laravel-admin

Schema::create('form_fields', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('form_id');
    $table->string('name', 255);
    $table->string('rules', 255);
});
class Field extends Model
{
    protected $table = 'form_fields';
    protected $casts = ['rules' => 'json'];
}



md5-d08f03c26dad006e357941e93069c87b



class FormBuilderController extends \Illuminate\Routing\Controller
{
    protected function form()
    {
        return Admin::form(BuilderForm::class, function (Form $form) {
            $form->hasMany('fields', function(NestedForm $nestedForm) {
                //this field is getting and storing successfully
                $nestedForm->text('name');
                //this embed field is ignoring...
                $nestedForm->embeds('rules'), function($form){
                    $form->switch('test');
                });
            });
        });
    }
}

Most helpful comment

workaround

Admin/Extensions/NestedEmbeds.php

<?php
namespace App\Admin\Extensions;


use Encore\Admin\Form\EmbeddedForm;
use Encore\Admin\Form\Field\Embeds;

class NestedEmbeds extends Embeds
{
    protected $view = 'admin::form.embeds';

    protected function buildEmbeddedForm()
    {
        $form = new EmbeddedForm($this->elementName);

        $form->setParent($this->form);

        call_user_func($this->builder, $form);

        $form->fill($this->getEmbeddedData());

        return $form;
    }
}

Admin/bootstrap.php

Encore\Admin\Form::extend('nestedEmbeds', \App\Admin\Extensions\NestedEmbeds::class);
class FormBuilderController extends \Illuminate\Routing\Controller
{
    protected function form()
    {
        return Admin::form(BuilderForm::class, function (Form $form) {
            $form->hasMany('fields', function(NestedForm $nestedForm) {
                //this field is getting and storing successfully
                $nestedForm->text('name');
                //this embed field is ignoring...
                $nestedForm->nestedEmbeds('rules'), function($form){
                    $form->switch('test');
                });
            });
        });
    }
}

All 4 comments

same problem

workaround

Admin/Extensions/NestedEmbeds.php

<?php
namespace App\Admin\Extensions;


use Encore\Admin\Form\EmbeddedForm;
use Encore\Admin\Form\Field\Embeds;

class NestedEmbeds extends Embeds
{
    protected $view = 'admin::form.embeds';

    protected function buildEmbeddedForm()
    {
        $form = new EmbeddedForm($this->elementName);

        $form->setParent($this->form);

        call_user_func($this->builder, $form);

        $form->fill($this->getEmbeddedData());

        return $form;
    }
}

Admin/bootstrap.php

Encore\Admin\Form::extend('nestedEmbeds', \App\Admin\Extensions\NestedEmbeds::class);
class FormBuilderController extends \Illuminate\Routing\Controller
{
    protected function form()
    {
        return Admin::form(BuilderForm::class, function (Form $form) {
            $form->hasMany('fields', function(NestedForm $nestedForm) {
                //this field is getting and storing successfully
                $nestedForm->text('name');
                //this embed field is ignoring...
                $nestedForm->nestedEmbeds('rules'), function($form){
                    $form->switch('test');
                });
            });
        });
    }
}

@shinbashi
Worked fine!! Nice!

@shinbashi Thanks, man!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joernroeder picture joernroeder  ·  3Comments

piian picture piian  ·  3Comments

evans-kim picture evans-kim  ·  3Comments

amun1303 picture amun1303  ·  3Comments

greentornado picture greentornado  ·  3Comments