To handle multiple shops with different product databases in a single Laravel application, you can use a multi-tenancy approach. This allows you to isolate data for each shop while using the same codebase. Here’s a step-by-step guide to achieve this:
Step 1: Install a Multi-Tenancy Package
One of the most popular packages for multi-tenancy in Laravel is stancl/tenancy. You can install it via Composer:
composer require stancl/tenancy
Step 2: Publish the Configuration
After installing the package, publish the configuration files:
php artisan vendor:publish --tag=tenancy
Step 3: Configure the Tenancy
Open the config/tenancy.php file and configure it according to your needs. For example, you can set the database connection to be used for tenants:
'tenant_database_connection_name' => 'tenant',
Step 4: Set Up Tenant Databases
You need to create separate databases for each shop. For example, you might have shop_a and shop_b databases. Configure these databases in your config/database.php:
'connections' => [
'tenant' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => null, // This will be set dynamically
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
// Other connections...
],
Step 5: Create Tenant Models and Migrations
Create a Tenant model to manage your shops:
php artisan make:model Tenant -m
In the migration file, define the schema for the tenants table:
Schema::create('tenants', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('domain')->unique();
$table->timestamps();
});
Step 6: Set Up Tenant Identification
You need to identify the tenant based on the request. This can be done using middleware. Create a middleware to identify the tenant:
php artisan make:middleware IdentifyTenant
In the middleware, set the tenant based on the request:
public function handle($request, Closure $next)
{
$tenant = Tenant::where('domain', $request->getHost())->firstOrFail();
tenancy()->initialize($tenant);
return $next($request);
}
Register the middleware in app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
// Other middleware...
\App\Http\Middleware\IdentifyTenant::class,
],
];
Step 7: Use Tenant Connection in Models
In your models, use the tenant connection:
class Product extends Model
{
protected $connection = 'tenant';
}
Step 8: Migrate Tenant Databases
To migrate the tenant databases, you can use the tenants:migrate command:
php artisan tenants:migrate
Step 9: Manage Products for Each Shop
Now, you can manage products for each shop separately. When a request is made, the tenant will be identified, and the corresponding database will be used.
Example Usage
When you create or fetch products, the correct tenant database will be used:
$products = Product::all(); // This will fetch products from the tenant's database
By following these steps, you can effectively manage multiple shops with different product databases within a single Laravel application.