Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Laravel 5.5 use a prefix on database tables

David Carr

Laravel Framework PHP & MySQL Tutorials

    I like to prefix my database tables to avoid any conflicts if using different applications within the same database ie have a CMS + a shop sharing the same database.

    This is easy to do with Laravel in `app/config/database.php` there is 

    'prefix' => '',

    You can put your prefix there or use the .env file.

    I’ll use the .enf file by adding the following:

    'prefix' => env('DB_PREFIX', ''),

    This will create a key called DB_PREFIX with a default value of none then in my .env file:

    Set the prefix


    Now all queries will look for tables that start with laravel_ before the table name, no further adjustments are required.

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.