# Accessing Tenant Domains Locally

## Overview
When you create a tenant, a domain is automatically generated in the format: `{slug}.{central_domain}`.

For example, if your tenant slug is `acme-corp` and your central domain is `127.0.0.1`, the tenant domain will be:
```
acme-corp.127.0.0.1
```

## Local Development Setup

### Option 1: Using lvh.me or localtest.me (Recommended)
These are wildcard DNS services that point to `127.0.0.1`:

1. **Update your `.env` file:**
   ```env
   APP_URL=http://localhost:8000
   CENTRAL_DOMAIN=lvh.me:8000
   ```

2. **Update `config/tenancy.php`:**
   ```php
   'central_domains' => [
       'lvh.me:8000',
       'localhost:8000',
   ],
   ```

3. **Access tenants:**
   - Central app: `http://localhost:8000`
   - Tenant: `http://acme-corp.lvh.me:8000`

### Option 2: Manually Edit Hosts File
Add each tenant domain to your `/etc/hosts` file:

```bash
# Edit hosts file
sudo nano /etc/hosts

# Add these lines for each tenant:
127.0.0.1 acme-corp.localhost
127.0.0.1 another-tenant.localhost
```

Then access via:
- `http://acme-corp.localhost:8000`

### Option 3: Using Local DNS Server (Advanced)
Set up dnsmasq to handle wildcard domains automatically.

## Testing Tenant Access

1. **Create a tenant** via admin panel
2. **Check the domain** in the database:
   ```bash
   php artisan tinker
   >>> \App\Models\Domain::latest()->first()
   ```

3. **Access the tenant login:**
   ```
   http://{tenant-slug}.lvh.me:8000/login
   ```

4. **Create a user** for the tenant (via seeder or manually in tenant database)

## Troubleshooting

### "Tenant could not be identified"
- Check that the domain exists in the `domains` table
- Verify `is_primary` is set to `1` (true)
- Verify `status` is set to `'active'`
- Check that `InitializeTenancyByDomain` middleware is applied to tenant routes

### Database not found
- Check that the tenant database was created
- Verify `database` column in `tenants` table matches actual database name
- Run: `SHOW DATABASES;` in MySQL to confirm

### Cannot access tenant login
- Ensure `routes/auth.php` is included in `routes/tenant.php`
- Check that tenant routes are wrapped with `InitializeTenancyByDomain::class` middleware
- Verify Laravel is running: `php artisan serve`

## Production Setup

For production, use proper DNS configuration:
1. Add wildcard DNS record: `*.yourdomain.com` → Your server IP
2. Configure SSL for wildcard domains
3. Update `config/tenancy.php` central_domains to your production domain
