Laravel 8 Tutorial - Databases Configuration

Published January 29, 2022

In this chapter we are going  to discuss Laravel 8 Databases. A database is the primary means for storing and retrieving data in a modern web application. Laravel 8 provides easy database interaction by combining the Eloquent ORM with a fluent query builder and raw SQL. Laravel 8 supports four databases: SQL Server, SQLite, PostgreSQL, and MySQL. In this chapter, we are going to look at the following:

  • How to configure databases in Laravel 8

  • Running Database Queries

  • Connecting to the Database CLI

1.      How to configure databases in Laravel 8

If you would like to configure your Laravel databases, then you'll need to do it in your application config/database.php configuration file.

It is in this file where you can define all the database connections as well as specify the type of database connection to be used by default. Most of these default database connections depend on the environment variables. However, you can freely change these database configurations to meet your local database needs.

Let's take a look at various ways in which you can configure databases supported by Laravel 8:

  • How to Configure an SQLite Database

By default, the SQLite databases are found in one of the files on your file system. However, if you want to create one, you can do so in your command prompt using the touch command: touch database/database.SQLite. After you successfully create your database, you are free to proceed to the environment variable configuration, where you can specify the path to the database in the DB_DATABASE environment variable

DB_CONNECTION = sqlite

DB_DATABASE =/absolute/path/to/database.sqlite

 

 

If you would like to enable the foreign key in your database, you can do so by setting the DB_FOREIGN_KEYS environment value to true as shown below

 

DB_FOREIGN_KEYS = true

 

  • How to Configure Microsoft SQL Server

Configuring the Microsoft SQL Server is pretty easy. However, to configure it, you need to have the pdo_sqlsrv and sqlsrv PHP extensions installed in your application. In addition, you will also need to have installed all the dependencies that the Microsoft SQL ODBC driver requires.

  • How to Configure Databases using URLs

By default, the database connections are configured by providing the configuration values such as hostname, the name of the database, the username, and the password.

However, not all database service providers support the database configurations using these variables. Some allow the URL database configuration, such as Heroku and AWS.  The following is an example of a database URL

 

mysql://root:password@127.0.0.1/forge?charset=UTF-8

 

You can configure your root, password, and other credentials in this URL

 

2.      Running Database Queries

Having successfully configured your database, you can now run various queries such as delete, insert, update, and select.

Select Query: Running a select query will basically select some data from your database.  To run this query, you need to use  the select method on the DB façade

use Illuminate\Support\Facades\DB;

$users = DB::select('select * from users');

foreach ($users as $user) {

    echo $user->name;

}

 

Insert Query: This query will insert some information into your database. To run an insert database statement, you need to use the insert method on the DB façade.  This method accepts the SQL query  as its first argument  and binding in the  second, as illustrated in the example below

use Illuminate\Support\Facades\DB;

DB::insert('insert into users (id, name) values (?, ?)', [1, 'Drew']);

 

  • Update Query:  This query updates the existing data in the databases. The update method is used to update the existing database records. 

example

use Illuminate\Support\Facades\DB;

$affected = DB::update(

    'update student set marks = 80 where name = ?',

    ['Elvis']

);

 

  • Delete Query: This query deletes all the selected records from the database.  To run this query, we use the delete method.

example

use Illuminate\Support\Facades\DB;

$deleted = DB::delete('delete from student');

 

3.      Connecting to the Database CLI

To connect to the database CLI, you only have to use the DB Artisan command

php artisan db

 

It can also be useful to provide a name for the database connection if you are using a database connection other than the default one.

example

 

php artisan db MySQL

 

Conclusion

This chapter taught us how to set up Laravel databases and connect to various databases. In the next chapter, we'll be looking at Laravel views

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

338 Views