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 skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting danya.dk