{"id":19300053,"url":"https://github.com/kirschbaum-development/laravel-couchbase","last_synced_at":"2026-05-17T10:32:33.942Z","repository":{"id":236299807,"uuid":"772229240","full_name":"kirschbaum-development/laravel-couchbase","owner":"kirschbaum-development","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-26T13:16:08.000Z","size":109,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-01-05T22:42:06.886Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/kirschbaum-development.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-14T19:30:13.000Z","updated_at":"2024-04-26T13:03:43.000Z","dependencies_parsed_at":"2024-04-26T14:28:02.353Z","dependency_job_id":"96671580-f5fa-4488-b59d-a87de6bc4551","html_url":"https://github.com/kirschbaum-development/laravel-couchbase","commit_stats":null,"previous_names":["kirschbaum-development/laravel-couchbase"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kirschbaum-development%2Flaravel-couchbase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kirschbaum-development%2Flaravel-couchbase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kirschbaum-development%2Flaravel-couchbase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kirschbaum-development%2Flaravel-couchbase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kirschbaum-development","download_url":"https://codeload.github.com/kirschbaum-development/laravel-couchbase/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240401361,"owners_count":19795535,"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":[],"created_at":"2024-11-09T23:13:32.555Z","updated_at":"2026-05-17T10:32:28.902Z","avatar_url":"https://github.com/kirschbaum-development.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Laravel Couchbase\n===============\n\nAn Eloquent model and Query builder with support for Couchbase, using the original Laravel API. *This library extends the original Laravel classes, so it uses exactly the same methods.*\n\n## Credits\n- Created from previous work of \n  * [friendsofcat/laravel-couchbase](https://github.com/friendsofcat/laravel-couchbase)\n  * [ORT-Interactive-GmbH/laravel-couchbase](https://github.com/ORT-Interactive-GmbH/laravel-couchbase)\n- [SDK references](https://docs.couchbase.com/sdk-api/couchbase-php-client/namespaces/couchbase.html)\n\nTable of contents\n-----------------\n* [Installation](#installation)\n* [Usage](#usage)\n* [Eloquent](#eloquent)\n* [Optional: Alias](#optional-alias)\n* [Query Builder](#query-builder)\n* [Schema](#schema)\n* [Extensions](#extensions)\n* [Troubleshooting](#troubleshooting)\n* [Examples](#examples)\n\nInstallation\n------------\n\n## Couchbase compatibility\n\n| Couchbase | Eloquent driver | Php |\n| --- | --- | --- |\n| 6.x | 1.x | ^7.4 |\n| 7.x | 2.x | ^8.0 |\n\nMake sure you have the Couchbase PHP driver installed. You can find installation instructions at http://developer.couchbase.com/documentation/server/current/sdk/php/start-using-sdk.html\n\nInstallation using composer:\n\n```\ncomposer require friendsofcat/laravel-couchbase\n```\n\nAnd add the service provider in `config/app.php`:\n\n```php\nFriendsOfCat\\Couchbase\\CouchbaseServiceProvider::class,\n```\n\nFor usage with [Lumen](http://lumen.laravel.com), add the service provider in `bootstrap/app.php`. In this file, you will also need to enable Eloquent. You must however ensure that your call to `$app-\u003ewithEloquent();` is **below** where you have registered the `CouchbaseServiceProvider `:\n\n```php\n$app-\u003eregister(FriendsOfCat\\Couchbase\\CouchbaseServiceProvider::class);\n\n$app-\u003ewithEloquent();\n```\n\nThe service provider will register a couchbase database extension with the original database manager. There is no need to register additional facades or objects. When using couchbase connections, Laravel will automatically provide you with the corresponding couchbase objects.\n\nFor usage outside Laravel, check out the [Capsule manager](https://github.com/illuminate/database/blob/master/README.md) and add:\n\n```php\n$capsule-\u003egetDatabaseManager()-\u003eextend('couchbase', function($config) {\n    return new FriendsOfCat\\Couchbase\\Connection($config);\n});\n```\n\n\nUsage\n-------------\n\n```bash\ncomposer require friendsofcat/laravel-couchbase\n```\n\n- And add a new couchbase connection:\n\n```php\n'couchbase' =\u003e [\n    'driver'   =\u003e 'couchbase',\n    'host'     =\u003e env('DB_HOST', 'localhost'),\n    'port'     =\u003e env('DB_PORT', 8091),\n    'bucket'   =\u003e env('DB_DATABASE'),\n    'username' =\u003e env('DB_USERNAME'),\n],\n```\n\n- You can connect to multiple servers or replica sets with the following configuration:\n\n```php\n'couchbase' =\u003e [\n    'driver'   =\u003e 'couchbase',\n    'host'     =\u003e ['host1', 'host2'],\n    'port'     =\u003e env('DB_PORT', 8091),\n    'bucket'   =\u003e env('DB_DATABASE'),\n    'username' =\u003e env('DB_USERNAME'),\n],\n```\n\n- Model usage\n```php\n\u003c?php\n\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass User extends Eloquent {\n  $connection = 'couchbase';\n}\n```\nMore on that on [eloquent section below](#eloquent)\n\nCommands\n---\n couchbase\n  couchbase:bucket:create                                  Create a bucket.\n  couchbase:bucket:create-primary-index                    Create index on bucket to allow performing nql queries\n  couchbase:bucket:delete                                  Delete a bucket.\n  couchbase:bucket:flush                                   Remove all data on bucket (clear database).\n  couchbase:bucket:list                                    List available buckets on cluster\n  couchbase:cluster:init                                   Initialize couchbase cluster before usage\n\n\nBefore usage in Laravel\n---\nNote that all the following commands can be done on the browser at `localhost:8091`\n- Init cluster before any usage\n```bash\nphp artisan couchbase:cluster:init\n```\n- Create a bucket\n```bash\nphp artisan couchbase:bucket:create --bucket canvas\n```\n\n\nEloquent\n--------\n\nThis package includes a Couchbase enabled Eloquent class that you can use to define Models for corresponding collections.\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass User extends Eloquent {}\n```\n\nAs Couchbase does not provide the concept of tables, documents will instead be defined by a property called `_type`. Like the original Eloquent, the lower-casem plural name of the class will be used as the \"table\" name and will be placed inside the `_type` property of each document.\n\nYou may specify a custom type (alias for table) by defining a `table` property on your model:\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass User extends Eloquent {\n\n    protected $table = 'my_users';\n\n}\n```\n\n**NOTE:** Eloquent will also assume that each collection has a primary key column named `_id`. You may define a `primaryKey` property to override this convention. Likewise, you may define a `connection` property to override the name of the database connection that should be used when utilizing the model.\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass MyModel extends Eloquent {\n\n    protected $connection = 'couchbase';\n\n}\n```\n\nEverything else (should) work just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent\n\n### Optional: Alias\n\nYou may also register an alias for the Couchbase model by adding the following to the alias array in `config/app.php`:\n\n```php\n'CouchbaseModel'       =\u003e 'FriendsOfCat\\Couchbase\\Eloquent\\Model',\n```\n\nThis will allow you to use the registered alias like:\n\n```php\nclass MyModel extends CouchbaseModel {}\n```\n\nQuery Builder\n-------------\n\nThe database driver plugs right into the original query builder. When using couchbase connections, you will be able to build fluent queries to perform database operations.\n\n\n```php\n$users = DB::table('users')-\u003eget();\n\n$user = DB::table('users')-\u003ewhere('name', 'John')-\u003efirst();\n```\n\nIf you did not change your default database connection, you will need to specify it when querying.\n\n```php\n$user = DB::connection('couchbase')-\u003etable('users')-\u003eget();\n```\n\nRead more about the query builder on http://laravel.com/docs/queries\n\nSchema\n------\nAs this Couchbase driver implementation uses a single bucket for all documents, a Schema builder is not implemented.\n\n\nExamples\n--------\n\n### Basic Usage\n\n**Retrieving All Models**\n\n```php\n$users = User::all();\n```\n\n**Retrieving A Record By Primary Key**\n\n```php\n$user = User::find('517c43667db388101e00000f');\n```\n\n**Wheres**\n\n```php\n$users = User::where('votes', '\u003e', 100)-\u003etake(10)-\u003eget();\n```\n\n**Or Statements**\n\n```php\n$users = User::where('votes', '\u003e', 100)-\u003eorWhere('name', 'John')-\u003eget();\n```\n\n**And Statements**\n\n```php\n$users = User::where('votes', '\u003e', 100)-\u003ewhere('name', '=', 'John')-\u003eget();\n```\n\n**Using Where In With An Array**\n\n```php\n$users = User::whereIn('age', [16, 18, 20])-\u003eget();\n```\n\n**Using Where Between**\n\n```php\n$users = User::whereBetween('votes', [1, 100])-\u003eget();\n```\n\n**Where null**\n\n```php\n$users = User::whereNull('updated_at')-\u003eget();\n```\n\n**Where is missing**\n\n```php\n$users = User::whereIsMissing('updated_at')-\u003eget();\n```\n\n**Where is valued**\n\n```php\n$users = User::whereIsValued('updated_at')-\u003eget();\n```\n\n**Order By**\n\n```php\n$users = User::orderBy('name', 'desc')-\u003eget();\n```\n\n**Offset \u0026 Limit**\n\n```php\n$users = User::skip(10)-\u003etake(5)-\u003eget();\n```\n\n**Distinct**\n\nDistinct requires a field for which to return the distinct values.\n\n```php\n$users = User::distinct()-\u003eget(['name']);\n// or\n$users = User::distinct('name')-\u003eget();\n```\n\nDistinct can be combined with **where**:\n\n```php\n$users = User::where('active', true)-\u003edistinct('name')-\u003eget();\n```\n\n**Advanced Wheres**\n\n```php\n$users = User::where('name', '=', 'John')-\u003eorWhere(function($query)\n    {\n        $query-\u003ewhere('votes', '\u003e', 100)\n              -\u003ewhere('title', '!=', 'Admin');\n    })\n    -\u003eget();\n```\n\n**Group By**\n\nSelected columns that are not grouped will be aggregated with the $last function.\n\n```php\n$users = Users::groupBy('title')-\u003eget(['title', 'name']);\n```\n\n**Aggregation**\n\n```php\n$total = Order::count();\n$price = Order::max('price');\n$price = Order::min('price');\n$price = Order::avg('price');\n$total = Order::sum('price');\n```\n\nAggregations can be combined with **where**:\n\n```php\n$sold = Orders::where('sold', true)-\u003esum('price');\n```\n\n**Like**\n\n```php\n$user = Comment::where('body', 'like', '%spam%')-\u003eget();\n```\n**NOTE**: Like checks in Couchbase are case sensitive\n\n**Incrementing or decrementing a value of a column**\n\nPerform increments or decrements (default 1) on specified attributes:\n\n```php\nUser::where('name', 'John Doe')-\u003eincrement('age');\nUser::where('name', 'Jaques')-\u003edecrement('weight', 50);\n```\n\nThe number of updated objects is returned:\n\n```php\n$count = User-\u003eincrement('age');\n```\n\nYou may also specify additional columns to update:\n\n```php\nUser::where('age', '29')-\u003eincrement('age', 1, ['group' =\u003e 'thirty something']);\nUser::where('bmi', 30)-\u003edecrement('bmi', 1, ['category' =\u003e 'overweight']);\n```\n\n**Soft deleting**\n\nWhen soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. To enable soft deletes for a model, apply the SoftDeletingTrait to the model:\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\SoftDeletes;\n\nclass User extends Eloquent {\n\n    use SoftDeletes;\n\n    protected $dates = ['deleted_at'];\n\n}\n```\n\nFor more information check http://laravel.com/docs/eloquent#soft-deleting\n\n### Inserts, updates and deletes\n\nInserting, updating and deleting records works just like the original Eloquent.\n\n**Saving a new model**\n\n```php\n$user = new User;\n$user-\u003ename = 'John';\n$user-\u003esave();\n```\n\nYou may also use the create method to save a new model in a single line:\n\n```php\nUser::create(['name' =\u003e 'John']);\n```\n\n**Updating a model**\n\nTo update a model, you may retrieve it, change an attribute, and use the save method.\n\n```php\n$user = User::first();\n$user-\u003eemail = 'john@foo.com';\n$user-\u003esave();\n```\n\n**Deleting a model**\n\nTo delete a model, simply call the delete method on the instance:\n\n```php\n$user = User::first();\n$user-\u003edelete();\n```\n\nOr deleting a model by its key:\n\n```php\nUser::destroy('517c43667db388101e00000f');\n```\n\nFor more information about model manipulation, check http://laravel.com/docs/eloquent#insert-update-delete\n\n### Relations\n\nSupported relations are:\n\n - hasOne\n - hasMany\n - belongsTo\n - belongsToMany\n - embedsOne\n - embedsMany\n\nExample:\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass User extends Eloquent {\n\n    public function items()\n    {\n        return $this-\u003ehasMany('Item');\n    }\n\n}\n```\n\nAnd the inverse relation:\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass Item extends Eloquent {\n\n    public function user()\n    {\n        return $this-\u003ebelongsTo('User');\n    }\n\n}\n```\n\nThe belongsToMany relation will not use a pivot \"table\", but will push id's to a __related_ids__ attribute instead. This makes the second parameter for the belongsToMany method useless. If you want to define custom keys for your relation, set it to `null`:\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass User extends Eloquent {\n\n    public function groups()\n    {\n        return $this-\u003ebelongsToMany('Group', null, 'user_ids', 'group_ids');\n    }\n\n}\n```\n\n\nOther relations are not yet supported, but may be added in the future. Read more about these relations on http://laravel.com/docs/eloquent#relationships\n\n### EmbedsMany Relations\n\nIf you want to embed Models, rather than referencing them, you can use the `embedsMany` relation. This relation is similar to the `hasMany` relation, but embeds the Models inside the parent object.\n\n**REMEMBER**: these relations return Eloquent collections, they don't return query builder objects!\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass User extends Eloquent {\n\n    public function books()\n    {\n        return $this-\u003eembedsMany('Book');\n    }\n\n}\n```\n\nYou access the embedded Models through the dynamic property:\n\n```php\n$books = User::first()-\u003ebooks;\n```\n\nThe inverse relation is auto*magically* available, you don't need to define this reverse relation.\n\n```php\n$user = $book-\u003euser;\n```\n\nInserting and updating embedded Models works similar to the `hasMany` relation:\n\n```php\n$book = new Book(['title' =\u003e 'A Game of Thrones']);\n\n$user = User::first();\n\n$book = $user-\u003ebooks()-\u003esave($book);\n// or\n$book = $user-\u003ebooks()-\u003ecreate(['title' =\u003e 'A Game of Thrones'])\n```\n\nYou can update embedded Models using their `save` method:\n\n```php\n$book = $user-\u003ebooks()-\u003efirst();\n\n$book-\u003etitle = 'A Game of Thrones';\n\n$book-\u003esave();\n```\n\nYou can remove an embedded model by using the `destroy` method on the relation, or the `delete` method on the model:\n\n```php\n$book = $user-\u003ebooks()-\u003efirst();\n\n$book-\u003edelete();\n// or\n$user-\u003ebooks()-\u003edestroy($book);\n```\n\nIf you want to add or remove an embedded model, without touching the database, you can use the `associate` and `dissociate` methods. To eventually write the changes to the database, save the parent object:\n\n```php\n$user-\u003ebooks()-\u003eassociate($book);\n\n$user-\u003esave();\n```\n\nLike other relations, embedsMany assumes the local key of the relationship based on the model name. You can override the default local key by passing a second argument to the embedsMany method:\n\n```php\nreturn $this-\u003eembedsMany('Book', 'local_key');\n```\n\nEmbedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: https://laravel.com/docs/master/collections\n\n### EmbedsOne Relations\n\nThe embedsOne relation is similar to the EmbedsMany relation, but only embeds a single model.\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass Book extends Eloquent {\n\n    public function author()\n    {\n        return $this-\u003eembedsOne('Author');\n    }\n\n}\n```\n\nYou access the embedded Models through the dynamic property:\n\n```php\n$author = Book::first()-\u003eauthor;\n```\n\nInserting and updating embedded Models works similar to the `hasOne` relation:\n\n```php\n$author = new Author(['name' =\u003e 'John Doe']);\n\n$book = Books::first();\n\n$author = $book-\u003eauthor()-\u003esave($author);\n// or\n$author = $book-\u003eauthor()-\u003ecreate(['name' =\u003e 'John Doe']);\n```\n\nYou can update the embedded model using the `save` method:\n\n```php\n$author = $book-\u003eauthor;\n\n$author-\u003ename = 'Jane Doe';\n$author-\u003esave();\n```\n\nYou can replace the embedded model with a new model like this:\n\n```php\n$newAuthor = new Author(['name' =\u003e 'Jane Doe']);\n$book-\u003eauthor()-\u003esave($newAuthor);\n```\n\n### MySQL Relations\n\nIf you're using a hybrid Couchbase and SQL setup, you're in luck! The model will automatically return a Couchbase- or SQL-relation based on the type of the related model. Of course, if you want this functionality to work both ways, your SQL-Models will need use the `FriendsOfCat\\Couchbase\\Eloquent\\HybridRelations` trait. Note that this functionality only works for hasOne, hasMany and belongsTo relations.\n\nExample SQL-based User model:\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\HybridRelations;\n\nclass User extends Eloquent {\n\n    use HybridRelations;\n\n    protected $connection = 'mysql';\n\n    public function messages()\n    {\n        return $this-\u003ehasMany('Message');\n    }\n\n}\n```\n\nAnd the Couchbase-based Message model:\n\n```php\nuse FriendsOfCat\\Couchbase\\Eloquent\\Model as Eloquent;\n\nclass Message extends Eloquent {\n\n    protected $connection = 'couchbase';\n\n    public function user()\n    {\n        return $this-\u003ebelongsTo('User');\n    }\n\n}\n```\n\n### Query Caching\n\nYou may easily cache the results of a query using the remember method:\n\n```php\n$users = User::remember(10)-\u003eget();\n```\n\n*From: http://laravel.com/docs/queries#caching-queries*\n\n### Query Logging\n\nBy default, Laravel keeps a log in memory of all queries that have been run for the current request. However, in some cases, such as when inserting a large number of rows, this can cause the application to use excess memory. To disable the log, you may use the `disableQueryLog` method:\n\n```php\nDB::connection()-\u003edisableQueryLog();\n```\n\n*From: http://laravel.com/docs/database#query-logging*\n\n## Testing\n- Create bucket\n- Create bucket primary index\n- Run tests\n- Use `RefreshDatabase` trait to clear database between tests\n\n\n## TODO\nThis is not a fully featured eloquent driver. There are still some areas where attention would be needed. Just to name few:\n- [ ] Implement _table_ concept using [collection/scope](https://docs.couchbase.com/sdk-api/couchbase-php-client/classes/Couchbase-CollectionManager.html) instead of `type`. This could improve performance\n- [ ] Test commands\n- [ ] Test pagination\n- [ ] Helper `uniqueId`\n- [ ] custom `RefreshDatabase` trait to recreate bucket\n- [ ] add `LazilyRefreshDatabase` instead of RefreshDatabase\nLead\n```php\n// RefreshDatabase\n$this-\u003eartisan(BucketDelete::class);\n$this-\u003eartisan(BucketCreate::class);\n$this-\u003eartisan(BucketCreatePrimaryIndex::class);\n// check hook afterApplicationCreated, beforeApplicationDestroyed\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkirschbaum-development%2Flaravel-couchbase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkirschbaum-development%2Flaravel-couchbase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkirschbaum-development%2Flaravel-couchbase/lists"}