PHP code example of gleman17 / laravel_tools

1. Go to this page and download the library: Download gleman17/laravel_tools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gleman17 / laravel_tools example snippets


use Gleman17\LaravelTools\Services\AIQueryService;

$aiQueryService = new AIQueryService();
$answer = $aiQueryService->getQuery('show me users that have posts without any comments');

return [
    'command_signatures' => [
        'remove_relationships' => 'tools:remove-relationships',
        'build_relationships' => 'tools:build-relationships',
        'compare_tables_with_models' => 'tools:check-tables',
        'list_models' => 'tools:list-models',
    ],
];

return [
    'ai_model' => 'gpt-4-0-mini',  // or your preferred model
];

use Gleman17\LaravelTools\Services\AIQueryService;

$queryService = new AIQueryService();

// Get SQL from natural language query
$query = "show me all users who have posted in the last month";
$sql = $queryService->getQuery($query);

// Execute the generated SQL
$results = DB::select($sql);

$synonyms = [
    'customer' => 'users',
    'article' => 'posts'
];

$query = "find all customers who have written articles";
$sql = $queryService->getQuery($query, $synonyms);

// Get tables involved in the query
$tables = $queryService->getQueryTables($query);

// Get reasoning behind table selection
$tableReasoning = $queryService->getTablesReasoning();

// Get reasoning behind SQL generation
$queryReasoning = $queryService->getQueryReasoning();

$additionalRules = "Always uery($query, $synonyms, $additionalRules);

$sql = $queryService->getQuery($query);
if ($sql === null) {
    // Handle the error case
    Log::error('Failed to generate SQL query');
    return false;
}

use Gleman17\LaravelTools\Services\DatabaseTableService;

$service = new DatabaseTableService();

$tables = $service->getDatabaseTables();
// Returns: ['users', 'posts', 'comments', ...]

$columns = $service->getTableColumns('users');
// Returns:
// [
//     'id' => [
//         'type' => 'integer',
//         'nullable' => false,
//         'default' => null,
//         'key' => 'PRI'
//     ],
//     'email' => [
//         'type' => 'varchar',
//         'nullable' => false,
//         'default' => null
//     ],
//     // ...
// ]

$metadata = $service->getMetadata();
// Returns:
// [
//     'users' => [
//         'id' => ['type' => 'integer', ...],
//         'email' => ['type' => 'varchar', ...],
//         // ...
//     ],
//     'posts' => [
//         // ...
//     ]
// ]

$modelName = $service->tableToModelName('blog_posts');
// Returns: 'BlogPost'

$relationName = $service->foreignKeyToRelationName('user_id');
// Returns: 'users'

try {
    $columns = $service->getTableColumns('users');
} catch (\RuntimeException $e) {
    Log::error('Failed to get table columns: ' . $e->getMessage());
    // Handle the error appropriately
}

use Gleman17\LaravelTools\Traits\HasEloquentStrings;

class YourClass
{
    use HasEloquentStrings;

    public function example()
    {
        $uglyQuery = '$query->where("active", true)->whereHas("posts", function($query) { $query->where("published", true)->whereNull("deleted_at"); })->orderBy("created_at")';
        
        $prettyQuery = $this->prettyPrintEloquent($uglyQuery);
        // Result:
        // $query
        //     ->where("active", true)
        //     ->whereHas("posts", function($query) {
        //         $query->where("published", true)
        //             ->whereNull("deleted_at");
        //     })
        //     ->orderBy("created_at")
    }
}

use Gleman17\LaravelTools\Traits\HasSqlStrings;

class YourClass
{
    use HasSqlStrings;

    public function example()
    {
        $uglySQL = "SELECT id, name, email, created_at FROM users WHERE active = 1 AND deleted_at IS NULL ORDER BY created_at DESC";
        
        $prettySQL = $this->prettyPrintSQL($uglySQL);
        // Result:
        // SELECT
        //     id,
        //     name,
        //     email,
        //     created_at
        // FROM users
        // WHERE
        //         active = 1
        //         AND deleted_at IS NULL
        // ORDER BY created_at DESC
    }
}
bash
php artisan vendor:publish --tag=gleman17-laravel-tools-config
bash
php artisan tools:build-relationships {startModel?} {endModel?} {--all}
bash
php artisan tools:build-relationships User Role
bash
php artisan tools:compare-tables {--make}
bash
php artisan tools:compare-tables --make
bash
php artisan tools:list-models
bash
php artisan tools:list-models
bash
php artisan tools:remove-relationships User Role
bash
php artisan tools:custom-commands
bash
php artisan tools:custom-commands --only