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

DB_PREFIX=laravel_

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

Copyright © 2006 - 2024 DC Blog - All rights reserved.