如何在laravel控制器中保存动态输入数据

时间:2020-10-30 12:55:14

标签: laravel vue.js

在将数据从vuejs保存到Laravel控制器时遇到了麻烦,我可以成功添加新的输入字段,但是无法将数据保存到数据库中

kidComponent.vue

<div class="pb-3">
    <h3 class="font-weight-bold">
        Number of Kids: <button @click="addNewChild"><i class="fa fa-list -ic2"></i></button>
    </h3>
</div>
<div class="columns pl-3" v-for="(kid, index) in kids" :key=" 'index' + index">
    <div class="column is-4">
        <b-field label="Name">
            <b-input v-model="kid.fullname" rounded></b-input>
        </b-field>
    </div>
    <div class="column is-4">
        <b-field label="Birthday">
            <b-datepicker v-model="kid.birthdate" rounded></b-datepicker>
        </b-field>
    </div>
    <div class="column is-4">
        <b-field label="Age">
            <b-input type="number" v-model="kid.age" rounded></b-input>
        </b-field>
    </div>
</div>

Vue方法

addNewChild() {
   this.kids.push({ 
       fullname: '',
       age: ''
   })
}

控制器

$kids = Kid::create($request->all());

1 个答案:

答案 0 :(得分:0)

您将以数组形式接收儿童数据。您必须遍历数组以插入数组的每个元素。

@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class MainDBConfig {
private final Environment env;

@Autowired
public MainDBConfig(Environment env) {
    this.env = env;
}

@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.main")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier("dataSource") DataSource dataSource
) {
    LocalContainerEntityManagerFactoryBean bean = builder
            .dataSource(dataSource)
            .packages("de.hspf.nxtgenmondial.backend.db.main.entity")
            .persistenceUnit("spring")
            .build();

    //see https://stackoverflow.com/questions/47245122/spring-hibernate-autowired-is-null-in-constraintvalidator
    HashMap<String, Object> map = new HashMap<>();
    map.put("javax.persistence.validation.factory", localValidatorFactoryBean());
    bean.setJpaPropertyMap(map);

    return bean;
}

@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
        @Qualifier("entityManagerFactory") EntityManagerFactory
                entityManagerFactory
) {
    return new JpaTransactionManager(entityManagerFactory);
}


@Bean
@ConfigurationProperties(prefix = "spring.datasource.main")
public LocalSessionFactoryBuilder sessionFactoryBuilder() {
    LocalSessionFactoryBuilder sfb = new LocalSessionFactoryBuilder(dataSource());
    sfb.scanPackages("de.hspf.nxtgenmondial.backend.db.main.entity");
    // Hibernate/JPA properties
    sfb.addProperties(hibernateProperties());
    return sfb;
}

@Bean
public SessionFactory sessionFactory() {
    return sessionFactoryBuilder().buildSessionFactory();
}

private Properties hibernateProperties() {
    return new Properties() {
        {
            setProperty("hibernate.hbm2ddl.auto",
                    env.getProperty("spring.datasource.main.hibernate.ddl-auto"));
            setProperty("hibernate.dialect",
                    env.getProperty("spring.datasource.main.hibernate.dialect"));
        }
    };
}

@Bean
public AuditorAware<String> auditorProvider() {
    return new AuditorAwareImpl();
}

@Bean(name = "validator")
public LocalValidatorFactoryBean localValidatorFactoryBean() {
    return new LocalValidatorFactoryBean();
}
}

或者如果您想特别知道哪个值在哪里,可以这样写

foreach ($request->kids as $key => $value) {
    Kid::create($value);
}