Laravel

Laravel 5.6 use a prefix on database tables

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:

  1. Add profix directly in app/config/database.php
  2. 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.

Author: Danyal
I'm a skilled programmer specializing in Vue.js/Nuxt.js for front-end development and PHP Laravel for back-end solutions. I have a strong focus on API design and development, complemented by experience in web server setup and maintenance. My versatile expertise ensures seamless creation and maintenance of web applications, covering everything from intuitive user interfaces to robust server-side functionality. Passionate about coding and driven by a lifelong learning mindset, I invite you to explore more at danyal.dk.