Sunset over the southern part of the Atlantic Ocean

How to Set Up a Database Connection with Laravel Eloquent

Laravel Eloquent is a powerful Object-Relational Mapping (ORM) library that allows developers to work with databases in a more intuitive way. In order to use Laravel Eloquent, you need to set up a database connection within your Laravel application. In this article, we’ll cover the steps required to set up a database connection using Laravel Eloquent.

Step 1: Configure Database Settings

The first step in setting up a database connection with Laravel Eloquent is configuring your database settings in the .env file. This file is located at the root of your Laravel application and contains environment variables that are used by the application.

Open the .env file and locate the following section:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

In this section, you can specify the type of database you are using, the host name, port number, database name, username, and password. The example above shows the settings for a MySQL database running on the same machine as the Laravel application.

Step 2: Configure Database Driver

The second step is to configure the database driver in the config/database.php file. This file contains the configuration settings for all the database drivers that Laravel supports.

Locate the following section in the config/database.php file:

'default' => env('DB_CONNECTION', 'mysql'),

'connections' => [

    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        ...

In this section, you can see that the default driver is set to mysql, which matches the value we specified in the .env file. The connections array contains the configuration settings for each database driver. In this example, we’re using the mysql driver.

Step 3: Define the Model

The final step is to define the Model class that represents the database table you want to work with. This class extends the Illuminate\Database\Eloquent\Model class, which provides a set of methods for interacting with the database.

Here’s an example of a Model class that represents a table called “users”:

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'users';
    protected $fillable = ['name', 'email', 'password'];
}

In this example, we’ve defined a ” User ” model class that represents the “users” table. We’ve also specified that the “name”, “email”, and “password” columns are fillable, which means that we can set their values using the Model’s create() method.

Conclusion

Setting up a database connection with Laravel Eloquent is a straightforward process. By configuring your database settings in the .env file, specifying the database driver in the config/database.php file, and defining the Model class, you can start working with databases more intuitively. Laravel Eloquent provides a robust set of methods for querying and manipulating data, which can significantly simplify your development process.

By Louis M.

About the authorMy LinkedIn profile

Related Links: