an3/laravel-couchdb
Work-in-progress CouchDB integration for Laravel 5.x, providing an Eloquent-style model and query builder inspired by jenssegers/laravel-mongodb. Intended to let you interact with CouchDB using familiar Laravel ORM patterns.
Installation
composer require an3/laravel-couchdb
Publish the config file:
php artisan vendor:publish --provider="An3\LaravelCouchdb\CouchdbServiceProvider"
Configuration
Edit .env with CouchDB connection details:
COUCHDB_CONNECTION=default
COUCHDB_HOST=127.0.0.1
COUCHDB_PORT=5984
COUCHDB_USERNAME=admin
COUCHDB_PASSWORD=password
First Model
Extend An3\LaravelCouchdb\Eloquent\Model:
use An3\LaravelCouchdb\Eloquent\Model;
class User extends Model
{
protected $connection = 'default';
protected $database = 'my_app_db';
protected $primaryKey = '_id';
}
First Query
$users = User::all(); // Returns Collection
$user = User::find('user123');
// Create
$user = User::create([
'name' => 'John Doe',
'email' => 'john@example.com'
]);
// Read
$users = User::where('email', 'like', '%@example.com')->get();
// Update
$user->update(['name' => 'Updated Name']);
// Delete
$user->delete();
Basic Queries
User::where('name', 'John')
->orderBy('created_at', 'desc')
->limit(10)
->get();
Complex Conditions
User::where(function($query) {
$query->where('age', '>', 25)
->orWhere('status', 'active');
})->get();
Joins (via _include)
CouchDB lacks traditional joins, but you can use _include for document references:
User::with('_posts')->find('user123');
Leverage Eloquent events for side effects:
User::created(function($user) {
// Log or notify
});
User::updated(function($user) {
// Sync external systems
});
Define schema in up():
Schema::create('users', function($collection) {
$collection->string('_id', 36)->primary();
$collection->string('name');
$collection->string('email')->unique();
$collection->timestamps();
});
Run migrations:
php artisan migrate
$users = User::paginate(15); // Uses CouchDB's `_changes` feed
Use beginTransaction for atomic operations (CouchDB supports MangoIndex transactions):
DB::beginTransaction();
try {
User::create([...]);
Profile::create([...]);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
}
No Traditional Joins
_include or denormalize data.user_id in posts and fetch users separately.Primary Key Conflicts
_id if not provided. Override incrementing:
public $incrementing = false;
Case Sensitivity
ilike for case-insensitive searches:
User::where('name', 'ilike', '%john%')->get();
Bulk Operations
bulkInsert:
User::bulkInsert([['name' => 'A'], ['name' => 'B']]);
Enable Query Logging
Add to config/couchdb.php:
'log' => env('COUCHDB_LOG', true),
Check Raw Queries
Use toCouchQuery() to inspect generated queries:
$query = User::where('active', true)->toCouchQuery();
Common Errors
database config and permissions.username/password in .env.create/update.Custom Query Macros Add reusable query logic:
User::addGlobalScope('active', function($builder) {
$builder->where('active', true);
});
Observers Sync data to other systems:
class UserObserver {
public function saved(User $user) {
// Webhook or cache update
}
}
Register in AppServiceProvider:
User::observe(UserObserver::class);
Custom Accessors/Mutators Transform data on-the-fly:
public function getFullNameAttribute() {
return "{$this->first_name} {$this->last_name}";
}
Indexing Define Mango indexes for frequent queries:
Schema::createIndex('users', 'name', 'text');
Bulk Fetching
Use _all_docs with include_docs=true for large datasets:
User::allDocs(['include_docs' => true]);
Caching
Cache frequent queries with remember:
$users = User::where('role', 'admin')->remember(60)->get();
How can I help you explore Laravel packages today?