The Laravel framework is known for its expressive syntax and its ability to quickly create powerful web applications. In this article I will show you how to query your Laravel database using natural language and why you might want to do so.
One of the most powerful features of Laravel is its ability to query databases using natural language. This article will discuss the benefits of using natural language to query databases and provide an overview of how to do so in Laravel.
What is Natural Language Query?
Natural language query (NLQ) is a type of query language that allows users to interact with databases using natural language. This means that users can ask questions in plain English and the database will return the appropriate results.
NLQ is a powerful tool for developers as it allows them to quickly and easily query databases without having to write complex SQL queries.
Benefits of Natural Language Query
There are several benefits to using natural language query in Laravel.
- It is easier to use than SQL: Natural language query is much easier to use than SQL. This makes it ideal for developers who are not familiar with SQL or who want to quickly query a database without having to write complex queries.
- It is more intuitive: Natural language query is more intuitive than SQL. This makes it easier for users to understand the results of their queries and to quickly find the information they are looking for.
- It is more flexible: Natural language query is more flexible than SQL. This allows developers to quickly and easily query databases without having to write complex queries.
- It is more secure: Natural language query is more secure than SQL. This makes it ideal for applications that require a high level of security.
How to Query Your Laravel Database Using Natural Language
Querying your Laravel database using natural language is relatively straightforward. The first step is to install the Laravel Ask Package. This package provides an easy-to-use interface for querying databases using natural language.
You can install Laravel Ask using composer:
composer require beyondcode/laravel-ask-database
Once the package is installed, you can publish the package config file:
php artisan vendor:publish --tag="ask-database-config"
You then need to create an account with OpenAI and generate an API token. You then need to add this to your .env and .env.sample files.
OPEN_API_TOKEN=XXXX
Database Ask Query Examples
You can then use the ask method that is now available through the DB Laravel Facade to query your database for the data and return the results.
How many users signed up in March 2023?
$results = DB::ask('How many users signed up in March 2023?');
How many registered users have an email address provided by Gmail?
$results = DB::ask('How many users have a GMail email address?');
Ask DB is a tool that enables users to build natural language database queries using OpenAI’s GPT-3 technology. With Ask DB, users can query databases using simple English phrases, and the tool automatically generates the underlying SQL query for the user. This makes it easier for users to access data and get insights, without needing to write a complex SQL query.
Frequently Asked Questions
Can you restrict what tables are used by the Laravel Ask Package?
The code gets a list of all tables for your default database connection and there is no configuration option or trait you can add to your models to restrict them. Having a configuration option as an array of tables to exclude would be a nice addition.
The only way to restrict table access would be to create an new MySQL user something like, “laravel-ask-db” and set table specific privileges. You can then add a custom database connection for the restricted user by editing config/database.php.
The last step is to override the default connection that is used by the Laravel Ask Package by adding the following to your .env and .env.sample files:
ASK_DATABASE_DB_CONNECTION=laravel-ask-db
Conclusion
Natural language query is a powerful tool for developers who want to quickly and easily query databases without having to write complex SQL queries. It is easier to use than SQL, more intuitive, more flexible, and more secure. Querying your Laravel database using natural language is relatively straightforward and can be done using the Laravel Query Builder package.
Resources
- Laravel Queries – Laravel
- Laravel Query Builder – Tutorials Point
- Query Builder – Codecourse
- Laravel Query Builder Tutorial – YouTube
- Laravel Query Builder – Advanced Queries – YouTube