Localized database in laravel 5.2

Updated: Feb 05, 2025

Localized database in laravel 5.2

A localized database in Laravel 5.2 refers to a database setup where you have multiple identical databases, each with its own set of locale-specific data. This is particularly useful when building applications that support multiple languages or regions, as it allows you to store and manage data specific to each locale separately.

To set up a localized database in Laravel 5.2, you'll need to follow these steps:

  1. Create a new database for each locale: First, you'll need to create a new database for each locale you plan to support. You can use a tool like phpMyAdmin or the command-line MySQL client to create these databases. Make sure each database has the same schema as your main application database.

  2. Configure your Laravel application to use multiple databases: Next, you'll need to configure your Laravel application to use multiple databases. You can do this by adding multiple database connections to your .env file. For example:

DB_CONNECTION_en=mysql
DB_HOST_en=localhost
DB_PORT_en=3306
DB_DATABASE_en=myapp_en
DB_USERNAME_en=root
DB_PASSWORD_en=

DB_CONNECTION_es=mysql
DB_HOST_es=localhost
DB_PORT_es=3306
DB_DATABASE_es=myapp_es
DB_USERNAME_es=root
DB_PASSWORD_es=
  1. Create a new database connection factory: Laravel uses a factory to create database connections. You'll need to create a new factory for each locale. You can do this by creating a new file in the app/Database/Factories directory, for example LocaleDatabaseFactory.php. In this file, you'll need to define a new factory method for each locale database connection. For example:
<?php

namespace App\Database\Factories;

use Illuminate\Database\Eloquent\Factory;

class LocaleDatabaseFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @param  array  $attributes
     * @return array
     */
    public function en()
    {
        return [
            'connection' => 'en',
            'database' => env('DB_DATABASE_en'),
            'username' => env('DB_USERNAME_en'),
            'password' => env('DB_PASSWORD_en'),
            'host' => env('DB_HOST_en'),
            'port' => env('DB_PORT_en'),
            'prefix' => '',
        ];
    }

    public function es()
    {
        return [
            'connection' => 'es',
            'database' => env('DB_DATABASE_es'),
            'username' => env('DB_USERNAME_es'),
            'password' => env('DB_PASSWORD_es'),
            'host' => env('DB_HOST_es'),
            'port' => env('DB_PORT_es'),
            'prefix' => '',
        ];
    }
}
  1. Register the new factory: Next, you'll need to register the new factory with Laravel. You can do this by adding the following line to the register method in the AppServiceProvider.php file:
$this->app->singleton('App\Database\Factories\LocaleDatabaseFactory');
  1. Use the new factory to create database connections: Finally, you can use the new factory to create database connections when your application starts up. You can do this by adding the following lines to the boot method in the AppServiceProvider.php file:
use App\Database\Factories\LocaleDatabaseFactory;

public function boot()
{
    // ...

    $localeFactory = app(LocaleDatabaseFactory::class);

    foreach (config('app.locales') as $locale) {
        $connection = $localeFactory->{$locale}();

        DB::connection($locale)
            ->reconnect($connection);
    }
}

This code uses the LocaleDatabaseFactory to create a new database connection for each locale, and then sets up the Laravel database connection pool with each connection.

With these steps in place, your Laravel application will be able to use multiple databases, each with its own set of locale-specific data.