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 [
'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();
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
}
}