Voice-Driven CRUD: From Spoken Commands to Database Records with Laravel & Vue.js

Admin

Admin

Apr 21, 2025 1 month ago

Introduction

In today's fast-paced digital world, voice-driven interfaces are rapidly gaining traction, offering users a hands-free way to interact with applications. Voice commands can enhance accessibility, improve user experience, and streamline workflows. This tutorial explores how to build a simple CRUD (Create, Read, Update, Delete) application using Laravel and Vue.js that responds to spoken commands, demonstrating the potential of voice-driven technology in modern web development.

Prerequisites

Before we get started, make sure you have the following tools installed:

  • Node.js (for Vue.js): npm install -g npm
  • Laravel: composer global require laravel/installer
  • Vue CLI: npm install -g @vue/cli
  • MySQL (for database management)
  • Git (for version control)

Vue Setup

First, let's set up our Vue.js application to capture speech input. Create a new Vue project by running:

vue create voice-crud

Change into the project directory:

cd voice-crud

Next, install the necessary dependencies:

npm install axios

In your src/App.vue file, add the following code to capture speech input:

<template>
  <div>
    <h1>Voice-Driven CRUD</h1>
    <button @click="startListening">Start Listening</button>
    <p>Command: {{ command }}</p>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      command: ''
    };
  },
  methods: {
    startListening() {
      const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
      recognition.onresult = (event) => {
        this.command = event.results[0][0].transcript;
        this.sendCommand(this.command);
      };
      recognition.start();
    },
    async sendCommand(command) {
      await axios.post('/api/voice-command', { command });
      // Implement toasts and loading states here
    }
  }
};
</script>

This code initializes the speech recognition API and sends the recognized command to our Laravel backend.

Laravel Setup

Now, let's set up the Laravel backend. First, create a new Laravel project:

laravel new voice-crud-backend

Change into the project directory and create a controller:

php artisan make:controller VoiceCommandController

In your routes/api.php, add the following route:

Route::post('/voice-command', [VoiceCommandController::class, 'handleVoiceCommand']);

Next, implement the handleVoiceCommand method in VoiceCommandController.php:

public function handleVoiceCommand(Request $request)
{
    $command = $request->input('command');
    // Simple regex to determine action
    if (preg_match('/add (.+)/', $command, $matches)) {
        // Create record logic using Eloquent
    } elseif (preg_match('/update (.+)/', $command, $matches)) {
        // Update record logic
    } elseif (preg_match('/delete (.+)/', $command, $matches)) {
        // Delete record logic
    } elseif ($command === 'show all') {
        // Read records logic
    }
}

Use Eloquent to interact with your database based on the recognized command.

Database Schema

For our tasks, we need a database schema. Create a migration for the tasks table:

php artisan make:migration create_tasks_table

In the migration file, define the schema:

public function up()
{
    Schema::create('tasks', function (Blueprint $table) {
        $table->id();
        $table->string('task_name');
        $table->timestamps();
    });
}

Then, create a model for the tasks:

php artisan make:model Task

This model will allow you to interact with the tasks table using Eloquent ORM.

UX Enhancements

To improve user experience, consider adding toast notifications for success or error messages when a command is processed. Additionally, implement loading states to indicate that the application is processing a request.

this.$bvToast.toast('Command executed successfully!', { variant: 'success' });

By using libraries like BootstrapVue, you can easily integrate toasts and loading indicators into your Vue app.

Conclusion

In this tutorial, we explored how to create a voice-driven CRUD application using Laravel and Vue.js. By integrating speech recognition, we can enhance user interaction and accessibility. As a next step, consider expanding the application with more complex commands or integrating additional features like user authentication and real-time updates.

Happy coding!

Related Articles

Voice-Driven CRUD: From Spoken Commands to Database Records with Laravel & Vue.js

Unlock the power of voice-driven interfaces in web applications. This step-by-st...

Apr 21, 2025

Unlocking Modular Code with Vue 3's Composition API

Discover how Vue 3's Composition API reorganizes component logic into reusable f...

Apr 21, 2025

Search

Share

Stay Updated

Subscribe to our newsletter to receive the latest articles and updates.

We respect your privacy. Unsubscribe at any time.