Using database table prefix can be helful avoiding conflicts, if you are using same database for multiple applications.
Laravel makes it quite simple by adding prefix in the following two files.
There are two approches:
- Add profix directly in app/config/database.php
- Add env variable in app/config/database.php and then define prefix in .env
Method 1: in your app ‘app/config/database.php’ look for following.
'prefix' => '',
Replace it by the desired table prefix
'prefix' => 'laravel_',
I personally don’t preffer this approach, because Laravel supports .env files and most of the configuration are done by .env.
Let’s take a look at 2nd approach.
Method 2: in your app ‘app/config/database.php’ look for following.
'prefix' => '',
Replace it by the desired table prefix
'prefix' => env('DB_TABLE_PREFIX', ''),
Then add this key with desired prefix in your app .env file.
'DB_TABLE_PREFIX='laravel_'
Remember to clear you cache when you modify .env file, because application has no idea about your newly added environment variable unless you clear config cache.
You can run following command:
php artisan config:clear
Important: There is no impact on migration table name, it will remain same without prefix, and laravel will add prefix to table when migrating tables.
Migration:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Sponsor: www.iExpenser.com
Free expense managing tool.