{"id":18309802,"url":"https://github.com/phpdevcommunity/php-query-builder","last_synced_at":"2025-10-06T01:41:01.710Z","repository":{"id":56966366,"uuid":"314897879","full_name":"phpdevcommunity/php-query-builder","owner":"phpdevcommunity","description":"A lightweight PHP query builder for easy database interactions","archived":false,"fork":false,"pushed_at":"2024-04-07T10:11:50.000Z","size":37,"stargazers_count":25,"open_issues_count":1,"forks_count":12,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-05T17:37:17.887Z","etag":null,"topics":["php","query-builder","sql"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phpdevcommunity.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-21T20:30:17.000Z","updated_at":"2025-03-05T10:49:15.000Z","dependencies_parsed_at":"2024-09-18T15:52:33.857Z","dependency_job_id":"686c6471-e448-4e4f-bb01-a10719dca51d","html_url":"https://github.com/phpdevcommunity/php-query-builder","commit_stats":null,"previous_names":["phpdevcommunity/php-query-builder","devcoder-xyz/php-query-builder"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/phpdevcommunity/php-query-builder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-query-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-query-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-query-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-query-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpdevcommunity","download_url":"https://codeload.github.com/phpdevcommunity/php-query-builder/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-query-builder/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278547771,"owners_count":26004772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-05T02:00:06.059Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","query-builder","sql"],"created_at":"2024-11-05T16:12:27.667Z","updated_at":"2025-10-06T01:41:01.682Z","avatar_url":"https://github.com/phpdevcommunity.png","language":"PHP","readme":"# PHP SQL Query Builder\n\nA lightweight PHP query builder for easy database interactions.\n\n[![Latest Stable Version](http://poser.pugx.org/devcoder-xyz/php-query-builder/v)](https://packagist.org/packages/devcoder-xyz/php-query-builder) [![Total Downloads](http://poser.pugx.org/devcoder-xyz/php-query-builder/downloads)](https://packagist.org/packages/devcoder-xyz/php-query-builder) [![Latest Unstable Version](http://poser.pugx.org/devcoder-xyz/php-query-builder/v/unstable)](https://packagist.org/packages/devcoder-xyz/php-query-builder) [![License](http://poser.pugx.org/devcoder-xyz/php-query-builder/license)](https://packagist.org/packages/devcoder-xyz/php-query-builder) [![PHP Version Require](http://poser.pugx.org/devcoder-xyz/php-query-builder/require/php)](https://packagist.org/packages/devcoder-xyz/php-query-builder)\n\n## Installation\n\nYou can install this library via [Composer](https://getcomposer.org/). Make sure your project meets the minimum PHP version requirement of 7.4 or higher.\n\n```bash\ncomposer require devcoder-xyz/php-query-builder\n```\n\n## Usage\n\nThe SQL Query Builder library allows you to build SQL queries fluently using an object-oriented approach. Here are some examples of usage:\n\n### Creating a SELECT Query\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create a SELECT query\n$query = QueryBuilder::select('name', 'email')\n    -\u003efrom('users')\n    -\u003ewhere('status = \"active\"')\n    -\u003eorderBy('name')\n    -\u003elimit(10);\n\necho $query; // Outputs: SELECT name, email FROM users WHERE status = \"active\" ORDER BY name LIMIT 10\n```\n\n### Types of SQL Joins with QueryBuilder\n\nThe SQL Query Builder library supports various types of JOIN operations to combine rows from multiple tables based on a related column between them. Below are examples of different JOIN types you can use with `QueryBuilder`:\n\n#### 1. INNER JOIN\n\nAn INNER JOIN returns records that have matching values in both tables.\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create a SELECT query with INNER JOIN\n$query = QueryBuilder::select('u.name', 'a.address')\n    -\u003efrom('users u')\n    -\u003einnerJoin('addresses a ON u.id = a.user_id');\n\necho $query; // Outputs: SELECT u.name, a.address FROM users u INNER JOIN addresses a ON u.id = a.user_id\n```\n\n#### 2. LEFT JOIN\n\nA LEFT JOIN returns all records from the left table (first table) and the matched records from the right table (second table). If there is no match, the result is NULL on the right side.\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create a SELECT query with LEFT JOIN\n$query = QueryBuilder::select('u.name', 'a.address')\n    -\u003efrom('users u')\n    -\u003eleftJoin('addresses a ON u.id = a.user_id');\n\necho $query; // Outputs: SELECT u.name, a.address FROM users u LEFT JOIN addresses a ON u.id = a.user_id\n```\n\n#### 3. RIGHT JOIN\n\nA RIGHT JOIN returns all records from the right table (second table) and the matched records from the left table (first table). If there is no match, the result is NULL on the left side.\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create a SELECT query with RIGHT JOIN\n$query = QueryBuilder::select('u.name', 'a.address')\n    -\u003efrom('users u')\n    -\u003erightJoin('addresses a ON u.id = a.user_id');\n\necho $query; // Outputs: SELECT u.name, a.address FROM users u RIGHT JOIN addresses a ON u.id = a.user_id\n```\n\n### Creating a SELECT Query with DISTINCT\n\nYou can use the `distinct()` method to specify a `SELECT DISTINCT` query with QueryBuilder.\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create a SELECT query with DISTINCT using QueryBuilder\n$query = QueryBuilder::select('name', 'email')\n    -\u003edistinct()\n    -\u003efrom('users')\n    -\u003ewhere('status = \"active\"')\n    -\u003eorderBy('name')\n    -\u003elimit(10);\n\necho $query; // Outputs: SELECT DISTINCT name, email FROM users WHERE status = \"active\" ORDER BY name LIMIT 10\n```\n\n### Creating a SELECT Query with GROUP BY\n\nYou can use the `groupBy()` method to specify a `GROUP BY` clause with QueryBuilder.\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create a SELECT query with GROUP BY using QueryBuilder\n$query = QueryBuilder::select('category_id', 'COUNT(*) as count')\n    -\u003efrom('products')\n    -\u003egroupBy('category_id');\n\necho $query; // Outputs: SELECT category_id, COUNT(*) as count FROM products GROUP BY category_id\n```\n\n### Creating a SELECT Query with HAVING Clause\n\nYou can use the `having()` method to specify a `HAVING` clause with QueryBuilder.\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create a SELECT query with HAVING using QueryBuilder\n$query = QueryBuilder::select('category_id', 'COUNT(*) as count')\n    -\u003efrom('products')\n    -\u003egroupBy('category_id')\n    -\u003ehaving('COUNT(*) \u003e 5');\n\necho $query; // Outputs: SELECT category_id, COUNT(*) as count FROM products GROUP BY category_id HAVING COUNT(*) \u003e 5\n```\n\n\n---\n\n### Creating an INSERT Query\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create an INSERT query\n$query = QueryBuilder::insert('users')\n    -\u003esetValue('name', '\"John Doe\"')\n    -\u003esetValue('email', '\"john.doe@example.com\"')\n    -\u003esetValue('status', '\"active\"');\n\necho $query; // Outputs: INSERT INTO users (name, email, status) VALUES (\"John Doe\", \"john.doe@example.com\", \"active\")\n```\n\n### Creating an UPDATE Query\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create an UPDATE query\n$query = QueryBuilder::update('users')\n    -\u003eset('status', '\"inactive\"')\n    -\u003ewhere('id = 123');\n\necho $query; // Outputs: UPDATE users SET status = \"inactive\" WHERE id = 123\n```\n\n### Creating an DELETE Query\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\n\n// Create a DELETE query\n$query = QueryBuilder::delete('users')\n    -\u003ewhere('status = \"inactive\"');\n\necho $query; // Outputs: DELETE FROM users WHERE status = \"inactive\"\n```\n\n### Creating a SELECT Query with Custom Expression\n\n```php\nuse DevCoder\\SqlBuilder\\QueryBuilder;\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example of a query with a custom expression\n$whereClause = Expr::greaterThan('age', '18');\n$query = QueryBuilder::select('name', 'email')\n    -\u003efrom('users')\n    -\u003ewhere($whereClause);\n\necho $query; // Outputs: SELECT name, email FROM users WHERE age \u003e 18\n```\n\n### List of Available Expressions (`Expr`)\n\nHere is a comprehensive list of available static methods in the `Expr` class along with examples demonstrating their usage:\n\n#### `Expr::equal(string $key, string $value)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate an equal comparison expression\n$equalExpr = Expr::equal('age', '30');\necho \"Equal Expression: $equalExpr\"; // Outputs: Equal Expression: age = 30\n```\n\n#### `Expr::notEqual(string $key, string $value)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate a not equal comparison expression\n$notEqualExpr = Expr::notEqual('status', '\"active\"');\necho \"Not Equal Expression: $notEqualExpr\"; // Outputs: Not Equal Expression: status \u003c\u003e \"active\"\n```\n\n#### `Expr::greaterThan(string $key, string $value)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate a greater than comparison expression\n$greaterThanExpr = Expr::greaterThan('salary', '50000');\necho \"Greater Than Expression: $greaterThanExpr\"; // Outputs: Greater Than Expression: salary \u003e 50000\n```\n\n#### `Expr::greaterThanEqual(string $key, string $value)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate a greater than or equal comparison expression\n$greaterThanEqualExpr = Expr::greaterThanEqual('points', '100');\necho \"Greater Than or Equal Expression: $greaterThanEqualExpr\"; // Outputs: Greater Than or Equal Expression: points \u003e= 100\n```\n\n#### `Expr::lowerThan(string $key, string $value)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate a lower than comparison expression\n$lowerThanExpr = Expr::lowerThan('price', '50');\necho \"Lower Than Expression: $lowerThanExpr\"; // Outputs: Lower Than Expression: price \u003c 50\n```\n\n#### `Expr::lowerThanEqual(string $key, string $value)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate a lower than or equal comparison expression\n$lowerThanEqualExpr = Expr::lowerThanEqual('quantity', '10');\necho \"Lower Than or Equal Expression: $lowerThanEqualExpr\"; // Outputs: Lower Than or Equal Expression: quantity \u003c= 10\n```\n\n#### `Expr::isNull(string $key)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate an IS NULL expression\n$isNullExpr = Expr::isNull('description');\necho \"IS NULL Expression: $isNullExpr\"; // Outputs: IS NULL Expression: description IS NULL\n```\n\n#### `Expr::isNotNull(string $key)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate an IS NOT NULL expression\n$isNotNullExpr = Expr::isNotNull('created_at');\necho \"IS NOT NULL Expression: $isNotNullExpr\"; // Outputs: IS NOT NULL Expression: created_at IS NOT NULL\n```\n\n#### `Expr::in(string $key, array $values)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate an IN expression\n$inExpr = Expr::in('category_id', [1, 2, 3]);\necho \"IN Expression: $inExpr\"; // Outputs: IN Expression: category_id IN (1, 2, 3)\n```\n\n#### `Expr::notIn(string $key, array $values)`\n\n```php\nuse DevCoder\\SqlBuilder\\Expression\\Expr;\n\n// Example: Generate a NOT IN expression\n$notInExpr = Expr::notIn('role', ['\"admin\"', '\"manager\"']);\necho \"NOT IN Expression: $notInExpr\"; // Outputs: NOT IN Expression: role NOT IN (\"admin\", \"manager\")\n```\n\nThese examples demonstrate how to use each `Expr` class method to generate SQL expressions for various comparison and conditional operations. Incorporate these methods into your SQL Query Builder usage to construct complex and precise SQL queries effectively.\n\n## Features\n\n- Fluent generation of SELECT, INSERT, UPDATE, and DELETE queries.\n- Secure SQL query building to prevent SQL injection vulnerabilities.\n- Support for WHERE, ORDER BY, GROUP BY, HAVING, LIMIT, and JOIN clauses.\n- Simplified methods for creating custom SQL expressions.\n\n## License\n\nThis library is open-source software licensed under the [MIT license](LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpdevcommunity%2Fphp-query-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpdevcommunity%2Fphp-query-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpdevcommunity%2Fphp-query-builder/lists"}