Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nerji's avatar
Level 3

Questions on implementing Laravel on existing databases

We have some old old PHP applications. They are nearing 20 years old and while we've kind of updated them from time to time I keep wondering if updating to Laravel would be wise. A full rewrite seems to be out of the question so I am curious about retrofitting this.

  1. We currently do our own auth. Would you bother installing Breeze and trying to squeeze our stuff in there (so use our databases and tables) or leave it as is?
  2. We currently use multiple databases in our current set up and a significant number of tables. Would I need to create a model for each one of those tables?

Any help would be greatly appreciated. Not sure if I want to take this on or just do something else.

1 like
3 replies
LaryAI's avatar
Level 58

Certainly! Transitioning an old PHP application to Laravel can be a daunting task, but it can also bring many benefits in terms of maintainability, security, and performance. Here are some steps and considerations for your specific questions:

1. Authentication

If you already have a custom authentication system, you have a couple of options:

Option A: Integrate with Laravel's Auth System

You can integrate your existing authentication system with Laravel's built-in authentication. This might involve some work but can be beneficial in the long run.

  • Install Laravel Breeze: Breeze is a simple implementation of Laravel's authentication system. You can install it using Composer:

    composer require laravel/breeze --dev
    php artisan breeze:install
    npm install && npm run dev
    php artisan migrate
    
  • Customize User Model: Modify the User model to match your existing user table structure. For example:

    class User extends Authenticatable
    {
        protected $table = 'your_existing_users_table';
        protected $primaryKey = 'your_existing_primary_key';
        // Add other necessary fields and relationships
    }
    
  • Update Auth Config: Update config/auth.php to use your custom user model and any other necessary configurations.

Option B: Keep Existing Auth System

If integrating with Laravel's auth system seems too complex, you can keep your existing authentication system and gradually migrate to Laravel's system over time.

2. Multiple Databases and Tables

Laravel supports multiple database connections and you don't necessarily need to create a model for each table unless you plan to interact with them using Eloquent.

Multiple Database Connections

You can define multiple database connections in config/database.php:

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        // other settings
    ],
    'mysql2' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST_SECOND', '127.0.0.1'),
        'database' => env('DB_DATABASE_SECOND', 'forge'),
        'username' => env('DB_USERNAME_SECOND', 'forge'),
        'password' => env('DB_PASSWORD_SECOND', ''),
        // other settings
    ],
    // Add more connections as needed
],

Using Multiple Connections in Models

You can specify which connection a model should use:

class SomeModel extends Model
{
    protected $connection = 'mysql2';
    protected $table = 'some_table';
}

Querying Without Models

If you don't want to create models for every table, you can use the DB facade to run queries:

use Illuminate\Support\Facades\DB;

$results = DB::connection('mysql2')->table('some_table')->get();

Conclusion

  • Authentication: Decide whether to integrate with Laravel's auth system or keep your existing system.
  • Multiple Databases: Configure multiple database connections and use them as needed.
  • Models: Create models for tables you frequently interact with, but use the DB facade for less frequently accessed tables.

This approach allows you to gradually transition to Laravel without a complete rewrite, making the process more manageable.

1 like
martinbean's avatar

We currently do our own auth. Would you bother installing Breeze and trying to squeeze our stuff in there (so use our databases and tables) or leave it as is?

@nerji It completely depends on what protocol you’re using to authenticate users. But Laravel’s built-in authentication component uses guards and providers, so even if you weren’t using email address and bcrypt-hashed passwords to authenticate users, you’d still be able to create a custom user provider to authenticate users. You’d then be able to use Auth::attempt($credentials) like normal, or with one of Laravel’s offerings such as Breeze, Fortify, Jetstream, etc.

We currently use multiple databases in our current set up and a significant number of tables. Would I need to create a model for each one of those tables?

Well if you want to use models to interact with the data then yes, you’ll need to create the models. Dump the database schema as it is currently (https://laravel.com/docs/11.x/migrations#squashing-migrations). You should then use migrations for any schema changes to the database(s) going forward.

1 like

Please or to participate in this conversation.