Skip to content

Laravel Integration

DevDb Pro provides deep integration with Laravel, offering specialized features for Eloquent models, query optimization, and factory generation.

Features Overview

  • Code Lens for Eloquent models
  • One-click factory generation with real data
  • MySQL query explainer for optimization
  • Context menu integration
  • Zero-config database discovery

Eloquent Model Code Lens

DevDb adds interactive Code Lens buttons directly above your Eloquent models.

View Table

Click "View table in DevDb" to:

  • Instantly open the model's underlying table
  • See live data from the database
  • Edit records directly

Example:

php
// Click "View table in DevDb" appears above the class
class User extends Model
{
    //...
}

Generate Factory

Click "Generate factory" to:

  • Auto-create a factory with real column names
  • Pre-fill with sample data from existing records
  • Match column types automatically

Example:

php
// Click "Generate factory" appears above the class
class Product extends Model
{
    //...
}

Generated factory:

php
<?php

namespace Database\Factories;

use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;

class ProductFactory extends Factory
{
    protected $model = Product::class;

    public function definition(): array
    {
        return [
            'name' => $this->faker->words(3, true),
            'description' => $this->faker->text(),
            'price' => $this->faker->randomFloat(2, 10, 1000),
            'stock_quantity' => $this->faker->numberBetween(0, 100),
            'category_id' => \App\Models\Category::factory(),
            'is_active' => $this->faker->boolean(),
            'created_at' => now(),
            'updated_at' => now(),
        ];
    }
}

Context Menu Integration

Right-click anywhere in your Laravel code to access DevDb features.

Open Table at Cursor

  1. Place cursor on a model class name
  2. Right-clickDevDb"Open table at cursor"
  3. Table opens in DevDb

Works with:

  • Model class names (User, Product, etc.)
  • Table names in strings ('users', 'products')
  • Eloquent relationships

Generate Factory from Cursor

  1. Place cursor on a model class name
  2. Right-clickDevDb"Generate Laravel Factory"
  3. Factory is created with real schema

Query Explainer

Optimize MySQL queries using visual EXPLAIN integration.

How It Works

DevDb integrates with MySQL Visual Explain to analyze query execution plans.

Usage

From Code Lens:

  1. Select a SQL query in your code
  2. Click "Explain query" Code Lens
  3. View explanation in browser or copy URL

From Context Menu:

  1. Select a SQL query
  2. Right-clickDevDb"Explain query"
  3. Analysis opens in browser

Supported Query Sources:

  • Eloquent query builder
  • DB facade queries
  • Raw SQL strings

Example:

php
// Select this query and click "Explain query"
DB::table('users')
    ->join('orders', 'users.id', '=', 'orders.user_id')
    ->where('users.is_active', true)
    ->get();

What You Get

The explainer shows:

  • Execution plan visualization
  • Index usage analysis
  • Join strategy details
  • Performance bottlenecks
  • Optimization suggestions

Zero-Config Database Discovery

DevDb automatically detects Laravel databases from .env configuration.

Supported Databases

  • SQLite (default local database)
  • MySQL/MariaDB
  • PostgreSQL
  • Microsoft SQL Server

How It Works

  1. DevDb reads your .env file
  2. Extracts database credentials
  3. Auto-connects to the database
  4. No manual configuration needed

Example .env:

env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_laravel_app
DB_USERNAME=root
DB_PASSWORD=secret

Laravel Sail Support

DevDb works with containerized Laravel (Sail):

  • Detects Docker containers
  • Reads docker-compose.yml
  • Auto-connects to containerized database
  • Dev container support included

Configuration

PHP Executable Path

For Laravel Artisan commands, configure PHP path:

  1. Open VS Code Settings
  2. Search for "DevDb PHP Executable"
  3. Set path (default: php)

Examples:

  • macOS with Homebrew: /opt/homebrew/bin/php
  • DDEV: ddev exec php
  • Custom: /usr/local/bin/php8.2

Code Lens Settings

Enable/disable Code Lens for text selection:

  1. Open Settings
  2. Search for "DevDb Code Lens"
  3. Toggle "Enable Code Lens for Text Selection"

Workflow Examples

Example 1: Build a Feature

  1. Create model (php artisan make:model Product)
  2. Run migration
  3. Open DevDb, click "Generate factory" Code Lens
  4. Review factory, adjust if needed
  5. Seed data using factory
  6. Browse data in DevDb

Example 2: Debug Slow Query

  1. Write Eloquent query in controller
  2. Select the query
  3. Click "Explain query" Code Lens
  4. Review execution plan
  5. Add indexes based on recommendations
  6. Rerun explanation to verify improvement

Example 3: Inspect Relationships

  1. Open a model with relationships
  2. Click "View table in DevDb"
  3. Browse related records
  4. Verify foreign keys in schema tab
  5. Edit test data directly

Tips & Best Practices

Factory Generation

  • Review generated factories before use
  • Customize Faker methods for realistic data
  • Add states for different scenarios
  • Use relationships with factory() method

Query Optimization

  • Start with EXPLAIN before optimizing
  • Focus on N+1 issues first
  • Add indexes based on EXPLAIN output
  • Re-test after changes

Model Development

  • Use Code Lens to quickly verify table structure
  • Check data directly from model file
  • Generate factories early for testing
  • Keep DevDb open while developing models

Team Collaboration

  • Share factory patterns with team
  • Document query optimizations in comments
  • Use consistent Faker methods across factories
  • Export seed data for shared fixtures

Troubleshooting

Code Lens Not Appearing

  1. Check PHP file is recognized (Laravel project)
  2. Verify class extends Model
  3. Restart VS Code extension host
  4. Check setting "Enable Code Lens" is on

Factory Generation Fails

  1. Verify PHP path in settings is correct
  2. Check Artisan is accessible
  3. Ensure factories folder exists
  4. Review output panel for errors

Query Explainer Issues

  1. Check query syntax is valid SQL
  2. Ensure MySQL database (explainer is MySQL-specific)
  3. Verify database connection is active
  4. Select full query including semicolon if needed

Next Steps

DevDb Pro - Zero-config database management for VS Code