Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Laravel 5.5 use a prefix on database tables

David Carr

Laravel Framework Tutorials PHP & MySQL

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 Your Logo Your Logo Your Logo

Become a sponsor

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


My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.