{"id":26292374,"url":"https://github.com/cuongdinhngo/lara-query-kit","last_synced_at":"2025-05-08T05:44:40.582Z","repository":{"id":56960246,"uuid":"385125310","full_name":"cuongdinhngo/lara-query-kit","owner":"cuongdinhngo","description":"Boost your Laravel models by Query Kit","archived":false,"fork":false,"pushed_at":"2021-07-20T03:47:09.000Z","size":10,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T17:14:36.459Z","etag":null,"topics":["filter","fulltext-search","laravel","model","php","upsert"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cuongdinhngo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-12T04:34:31.000Z","updated_at":"2023-11-01T03:58:24.000Z","dependencies_parsed_at":"2022-08-21T09:20:41.580Z","dependency_job_id":null,"html_url":"https://github.com/cuongdinhngo/lara-query-kit","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuongdinhngo%2Flara-query-kit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuongdinhngo%2Flara-query-kit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuongdinhngo%2Flara-query-kit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuongdinhngo%2Flara-query-kit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cuongdinhngo","download_url":"https://codeload.github.com/cuongdinhngo/lara-query-kit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253009824,"owners_count":21839713,"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","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":["filter","fulltext-search","laravel","model","php","upsert"],"created_at":"2025-03-15T01:35:36.281Z","updated_at":"2025-05-08T05:44:40.565Z","avatar_url":"https://github.com/cuongdinhngo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lara Query Kit\n\nLara Query Kit utilizes the Eloquent model. PHP Trait and Laravel local scope are used to implement this utilitiy\n\n### PHP Traits\n\nPHP only supports single inheritance: a child class can inherit only from one single parent.\n\nSo, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.\n\nTraits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).\n\n### Laravel local scope\n\nLocal scopes allow you to define common sets of constraints that you may easily re-use throughout your application. To define a scope, simply prefix an Eloquent model method with `scope`.\n\n\n## Installation \u0026 Configuration\n\n1-Install `cuongnd88/lara-query-kit` using Composer.\n\n```php\n\tcomposer require cuongnd88/lara-query-kit\n```\n\n2-Push `lara-query-kit` into your application.\n\n```php\n\tphp artisan vendor:publish --provider=\"Cuongnd88\\LaraQueryKit\\LaraQueryKitServiceProvider\"\n```\n\n3-`App\\Traits\\QueryKit.php` is already to pump your performance. Please add `QueryKit` into the model\n\n```php\n. . . .\nuse App\\Traits\\QueryKit;\n\nclass User extends Authenticatable\n{\n    use Notifiable;\n    use HasOtpAuth;\n    use HasGuardian;\n    use QueryKit;\n\n. . . .\n}\n```\n\n\n## Available methods\n\nLet discuss each method available on the Query Kit.\n\n- [insertDuplicate()](#insertDuplicate)\n- [getTableColumns()](#getTableColumns)\n- [exclude()](#exclude)\n- [filter()](#filter)\n- [searchFulltext()](#searchFulltext)\n\n### insertDuplicate\n\n_`insertDuplicate(array $data, array $insertKeys, array $updateKeys)`: Insert new rows or update existed rows._\n\n```php\n    public function upsert()\n    {\n        $data = [\n            ['name' =\u003e \"Dean Ngo\", 'email' =\u003e 'dinhcuongngo@gmail.com', 'mobile' =\u003e '84905005533', 'password' =\u003e Hash::make('123456')],\n            ['name' =\u003e \"Robert Neil\", 'email' =\u003e '1111@gmail.com', 'mobile' =\u003e '84905001122', 'password' =\u003e Hash::make('123456')],\n        ];\n        User::insertDuplicate($data, ['name', 'email', 'mobile', 'password'], ['name', 'email', 'mobile']);\n    }\n```\n\n### getTableColumns\n\n_`getTableColumns()`: Get the array of columns._\n\n```php\n    public function listTableColumns()\n    {\n        $columns = User::getTableColumns();\n        dump($columns);\n    }\n```\n\n### exclude\n\n_`exclude(array $columns)`: Retrieve a subset of the output data._\n\nYou should define which model attributes you want to exclude. You may do this using the `$excludable` property on the model.\n\n```php\n. . . .\nuse App\\Traits\\QueryKit;\n\nclass User extends Authenticatable\n{\n    use Notifiable;\n    use HasOtpAuth;\n    use HasGuardian;\n    use QueryKit;\n\n    protected $excludable = ['deleted_at', 'created_at', 'updated_at'];\n\n. . . .\n}\n```\n\n```php\n    public function listUsers()\n    {\n        $data = User::exclude()-\u003eget()-\u003etoArray();\n        dump($data);\n    }\n```\n\nOr pass a array of excludable columns as argument\n\n```php\n    public function listUsers()\n    {\n        $users = User::exclude(['deleted_at', 'created_at', 'updated_at'])\n                        -\u003eget()-\u003etoArray();\n        dump($users);\n    }\n```\n\n### filter\n\n_`filter(array $params)`: Get the result with filter conditions._\n\n\nYou may use the `fitler` method on a query builder instance to add `where` clauses to the query.\nThe `$filterable` property should contain an array of conditions that you want to execute searching. The key of filterable array corresponds to table columns, whereas the value is condition to call `where` clauses. The most basic condition requires two arguments:\n\n- The first argument is simple where clause such as: where, orWhere, whereBetween, whereNotBetween, whereIn, whereNotIn, whereNull, whereNotNull, orWhereNull , orWhereNotNull, whereDate, whereMonth, whereDay, whereYear, whereTime\n\n- The second argument is an operator, which can be any of the database's supported operators.\n\nExceptionally, the third argument is required with the `operator` is `LIKE`, it is for a specified pattern\n\nFor convenience, if you verify only column in `fiterable` property, the default clause is `where` with `=` operator\n\n```php\n. . . .\nuse App\\Traits\\QueryKit;\n\nclass User extends Authenticatable\n{\n    use Notifiable;\n    use HasOtpAuth;\n    use HasGuardian;\n    use QueryKit;\n\n    protected $filterable = [\n        'id' =\u003e ['whereBetween'],\n        'email',\n        'name' =\u003e ['orWhere', 'LIKE', '%{name}%'],\n    ];\n\n. . . .\n}\n```\n\n```php\n    public function filter()\n    {\n        $where = [\n            'id' =\u003e [1,5],\n            'email' =\u003e 'dinhcuongngo@gmail.com',\n            'name' =\u003e 'ngo',\n        ];\n\n        $data = User::-\u003efilter($where)-\u003eget()-\u003etoArray();\n    }\n```\n\nDynamically, you can call `filterableCondition()` and assign the filterable conditions\n\n```php\n    public function filter()\n    {\n        $filterable = [\n            'email',\n            'name' =\u003e ['orWhere', 'LIKE', '%{name}%'],\n            'deleted_at' =\u003e ['whereNull'],\n        ];\n\n        $where = [\n            'email' =\u003e 'dinhcuongngo@gmail.com',\n            'name' =\u003e 'ngo',\n            'deleted_at' =\u003e '',\n        ];\n\n        $data = User::filterableCondition($filterable)\n                        -\u003efilter($where)\n                        -\u003eget()\n                        -\u003etoArray();\n    }\n```\n\n### searchFulltext\n\n_`searchFulltext($value, $mode = NATURAL_LANGUAGE)`: Run full-text queries against character-based data in MySQL tables._\n\nThere are four modes of full-text searches:\n\n- `NATURAL_LANGUAGE` (is default): IN NATURAL LANGUAGE MODE\n- `NATURAL_LANGUAGE_QUERY`: IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION\n- `BOOLEAN_MODE`: IN BOOLEAN MODE\n- `QUERY_EXPANSION`: WITH QUERY EXPANSION\n\nThe `$searchable` property should contain an array of conditions that you search full-\n\n```php\n. . . .\nuse App\\Traits\\QueryKit;\n\nclass User extends Authenticatable\n{\n    use Notifiable;\n    use HasOtpAuth;\n    use HasGuardian;\n    use QueryKit;\n\n\n    protected $excludable = ['deleted_at', 'created_at', 'updated_at'];\n\n    protected $searchable = [\n        'name', 'address'\n    ];\n\n. . . .\n}\n```\n\n```php\n    public function search()\n    {\n        $data = User::searchFulltext('ngo')-\u003eexclude()-\u003eget()-\u003etoArray();\n        dump($data);\n    }\n```\n\nYou flexibly add matched columns by `searchableCols()`\n\n```php\n    public function search()\n    {\n        $data = User::searchableCols(['name', 'address'])\n                        -\u003esearchFulltext('ngo')\n                        -\u003eexclude()\n                        -\u003eget()\n                        -\u003etoArray();\n        dump($data);\n    }\n```\n\nYou must create a `full-text index` on the table before you run full-text queries on a table. The full-text index can include one or more character-based columns in the table.\n\n```php\nALTER TABLE `users` ADD FULLTEXT(`name`, `address`);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcuongdinhngo%2Flara-query-kit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcuongdinhngo%2Flara-query-kit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcuongdinhngo%2Flara-query-kit/lists"}