Download the PHP package friendsofcat/laravel-couchbase without Composer
On this page you can find all versions of the php package friendsofcat/laravel-couchbase. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-couchbase
Laravel Couchbase
An 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.
Credits
- Created from previous work of
- SDK references
Table of contents
- Installation
- Usage
- Eloquent
- Optional: Alias
- Query Builder
- Schema
- Extensions
- Troubleshooting
- Examples
Installation
Couchbase compatibility
Couchbase | Eloquent driver | Php |
---|---|---|
6.x | 1.x | ^7.4 |
7.x | 2.x | ^8.0 |
Make 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
Installation using composer:
And add the service provider in config/app.php
:
For usage with Lumen, 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->withEloquent();
is below where you have registered the CouchbaseServiceProvider
:
The 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.
For usage outside Laravel, check out the Capsule manager and add:
Usage
-
And add a new couchbase connection:
-
You can connect to multiple servers or replica sets with the following configuration:
- Model usage
More on that on eloquent section below
Commands
couchbase couchbase:bucket:create Create a bucket. couchbase:bucket:create-primary-index Create index on bucket to allow performing nql queries couchbase:bucket:delete Delete a bucket. couchbase:bucket:flush Remove all data on bucket (clear database). couchbase:bucket:list List available buckets on cluster couchbase:cluster:init Initialize couchbase cluster before usage
Before usage in Laravel
Note that all the following commands can be done on the browser at localhost:8091
-
Init cluster before any usage
- Create a bucket
Eloquent
This package includes a Couchbase enabled Eloquent class that you can use to define Models for corresponding collections.
As 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.
You may specify a custom type (alias for table) by defining a table
property on your model:
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.
Everything else (should) work just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
Optional: Alias
You may also register an alias for the Couchbase model by adding the following to the alias array in config/app.php
:
This will allow you to use the registered alias like:
Query Builder
The 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.
If you did not change your default database connection, you will need to specify it when querying.
Read more about the query builder on http://laravel.com/docs/queries
Schema
As this Couchbase driver implementation uses a single bucket for all documents, a Schema builder is not implemented.
Examples
Basic Usage
Retrieving All Models
Retrieving A Record By Primary Key
Wheres
Or Statements
And Statements
Using Where In With An Array
Using Where Between
Where null
Where is missing
Where is valued
Order By
Offset & Limit
Distinct
Distinct requires a field for which to return the distinct values.
Distinct can be combined with where:
Advanced Wheres
Group By
Selected columns that are not grouped will be aggregated with the $last function.
Aggregation
Aggregations can be combined with where:
Like
NOTE: Like checks in Couchbase are case sensitive
Incrementing or decrementing a value of a column
Perform increments or decrements (default 1) on specified attributes:
The number of updated objects is returned:
You may also specify additional columns to update:
Soft deleting
When 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:
For more information check http://laravel.com/docs/eloquent#soft-deleting
Inserts, updates and deletes
Inserting, updating and deleting records works just like the original Eloquent.
Saving a new model
You may also use the create method to save a new model in a single line:
Updating a model
To update a model, you may retrieve it, change an attribute, and use the save method.
Deleting a model
To delete a model, simply call the delete method on the instance:
Or deleting a model by its key:
For more information about model manipulation, check http://laravel.com/docs/eloquent#insert-update-delete
Relations
Supported relations are:
- hasOne
- hasMany
- belongsTo
- belongsToMany
- embedsOne
- embedsMany
Example:
And the inverse relation:
The 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
:
Other relations are not yet supported, but may be added in the future. Read more about these relations on http://laravel.com/docs/eloquent#relationships
EmbedsMany Relations
If 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.
REMEMBER: these relations return Eloquent collections, they don't return query builder objects!
You access the embedded Models through the dynamic property:
The inverse relation is automagically available, you don't need to define this reverse relation.
Inserting and updating embedded Models works similar to the hasMany
relation:
You can update embedded Models using their save
method:
You can remove an embedded model by using the destroy
method on the relation, or the delete
method on the model:
If 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:
Like 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:
Embedded 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
EmbedsOne Relations
The embedsOne relation is similar to the EmbedsMany relation, but only embeds a single model.
You access the embedded Models through the dynamic property:
Inserting and updating embedded Models works similar to the hasOne
relation:
You can update the embedded model using the save
method:
You can replace the embedded model with a new model like this:
MySQL Relations
If 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.
Example SQL-based User model:
And the Couchbase-based Message model:
Query Caching
You may easily cache the results of a query using the remember method:
From: http://laravel.com/docs/queries#caching-queries
Query Logging
By 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:
From: http://laravel.com/docs/database#query-logging
Testing
- Create bucket
- Create bucket primary index
- Run tests
- Use
RefreshDatabase
trait to clear database between tests
TODO
This is not a fully featured eloquent driver. There are still some areas where attention would be needed. Just to name few:
- [ ] Implement table concept using collection/scope instead of
type
. This could improve performance - [ ] Test commands
- [ ] Test pagination
- [ ] Helper
uniqueId
- [ ] custom
RefreshDatabase
trait to recreate bucket - [ ] add
LazilyRefreshDatabase
instead of RefreshDatabase Lead
All versions of laravel-couchbase with dependencies
illuminate/support Version ^7.0|^8.0|^9.0|^10.0
illuminate/container Version ^7.0|^8.0|^9.0|^10.0
illuminate/database Version ^7.0|^8.0|^9.0|^10.0
illuminate/events Version ^7.0|^8.0|^9.0|^10.0
ext-couchbase Version >=3.0