Implementing a dynamic form builder in Laravel and Vue.js involves creating a backend in Laravel for handling form configurations and submissions, as well as a frontend in Vue.js for rendering and interacting with the dynamic forms. Below is a basic guide to help you get started. Note that this is a simplified example, and you may need to adapt it based on your specific requirements.
Install Laravel:
csscomposer create-project --prefer-dist laravel/laravel dynamic-form-builder
Create a Form model and migration:
gophp artisan make:model Form -m
Modify the migration file (database/migrations/xxxx_xx_xx_create_forms_table.php
):
phppublic function up()
{
Schema::create('forms', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->json('fields'); // Store form fields as JSON
$table->timestamps();
});
}
Run migrations:
php artisan migrate
Create a FormController:
gophp artisan make:controller FormController
Define CRUD methods in FormController.php
:
php// app/Http/Controllers/FormController.php
public function index()
{
$forms = Form::all();
return response()->json($forms);
}
public function store(Request $request)
{
$form = Form::create($request->all());
return response()->json($form, 201);
}
public function show($id)
{
$form = Form::findOrFail($id);
return response()->json($form);
}
public function update(Request $request, $id)
{
$form = Form::findOrFail($id);
$form->update($request->all());
return response()->json($form, 200);
}
public function destroy($id)
{
Form::destroy($id);
return response()->json(null, 204);
}
Configure routes (routes/api.php
):
phpRoute::resource('forms', 'FormController');
Install Vue.js:
bashvue create dynamic-form-builder
Install Axios for HTTP requests:
bashcd dynamic-form-builder
npm install axios
Create a FormBuilder.vue
component:
html<!-- src/components/FormBuilder.vue -->
<template>
<div>
<form v-if="formFields.length > 0" @submit.prevent="submitForm">
<div v-for="field in formFields" :key="field.name">
<label :for="field.name">{{ field.label }}</label>
<input :type="field.type" :name="field.name" v-model="formValues[field.name]" />
</div>
<button type="submit">Submit</button>
</form>
</div>
</template>
<script>
export default {
props: ['formFields'],
data() {
return {
formValues: {},
};
},
methods: {
submitForm() {
// Send formValues to Laravel backend using Axios
axios.post('/api/forms', this.formValues)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
},
},
};
</script>
Create a FormList.vue
component:
html<!-- src/components/FormList.vue -->
<template>
<div>
<div v-for="form in forms" :key="form.id">
<router-link :to="{ name: 'form', params: { id: form.id } }">{{ form.name }}</router-link>
</div>
</div>
</template>
<script>
export default {
data() {
return {
forms: [],
};
},
created() {
// Fetch forms from Laravel backend using Axios
axios.get('/api/forms')
.then(response => {
this.forms = response.data;
})
.catch(error => {
console.error(error);
});
},
};
</script>
Configure Vue Router (src/router/index.js
):
javascriptimport Vue from 'vue';
import VueRouter from 'vue-router';
import FormBuilder from '@/components/FormBuilder.vue';
import FormList from '@/components/FormList.vue';
Vue.use(VueRouter);
const routes = [
{ path: '/forms', component: FormList },
{ path: '/form/:id', name: 'form', component: FormBuilder, props: true },
];
const router = new VueRouter({
routes,
});
export default router;
Modify src/App.vue
to use the router:
html<template>
<div id="app">
<router-view />
</div>
</template>
<script>
export default {
name: 'App',
};
</script>
Run the Vue.js app:
bashnpm run serve
Now, you have a basic setup for a dynamic form builder using Laravel for the backend and Vue.js for the frontend. You can further enhance and customize this example based on your specific needs.