PHP code example of robsonvn / laravel-couchdb

1. Go to this page and download the library: Download robsonvn/laravel-couchdb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

robsonvn / laravel-couchdb example snippets


Robsonvn\CouchDB\ServiceProvider::class,

'default' => env('DB_CONNECTION', 'couchdb'),

'couchdb' => [    
    'driver'   => 'couchdb',
    'type'     => env('DB_CONNECTION_TYPE', 'socket'),
    'host'     => env('DB_HOST', 'localhost'),
    'ip'       => env('DB_IP', null),
    'port'     => env('DB_PORT', '5984'),
    'dbname'   => env('DB_DATABASE', 'forge'),
    'user'     => env('DB_USERNAME', null),
    'password' => env('DB_PASSWORD', null),
    'logging'  => env('DB_LOGGING', false),
],

use Robsonvn\CouchDB\Eloquent\Model as Eloquent;

class Book extends Eloquent{}

use Robsonvn\CouchDB\Eloquent\Model as Eloquent;

class Book extends Eloquent{
  protected $collection = 'books_collection';
}

$users = DB::collection('users')->get();

$user = DB::collection('users')->where('name', 'John')->first();

$user = DB::connection('couchdb')->collection('users')->get();

Robsonvn\CouchDB\Auth\PasswordResetServiceProvider::class,

use Robsonvn\CouchDB\Auth\User as Authenticatable;

class User extends Authenticatable
{
}

'connections' => [
    'database' => [
        'driver' => 'couchdb',
        'table'  => 'jobs',
        'queue'  => 'default',
        'expire' => 60,
    ],

'failed' => [
    'database' => 'couchdb',
    'table'    => 'failed_jobs',
    ],

Robsonvn\CouchDB\CouchDBQueueServiceProvider::class,

$users = User::all();

$user = User::find('517c43667db388101e00000f');

$users = User::where('votes', '>', 100)->take(10)->get();

$users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();

$users = User::where('votes', '>', 100)->where('name', '=', 'John')->get();

$users = User::whereIn('age', [16, 18, 20])->get();

$users = User::whereBetween('votes', [1, 100])->get();

$users = User::whereNull('updated_at')->get();

$users = User::orderBy('name', 'desc')->get();

$users = User::skip(10)->take(5)->get();

$users = User::where('name', '=', 'John')->orWhere(function($query)
    {
        $query->where('votes', '>', 100)
              ->where('title', '<>', 'Admin');
    })
    ->get();

$user = Comment::where('body', 'like', '%spam%')->get();

$user = Comment::where('body', 'ilike', '%spam%')->get();

User::where('name', 'John Doe')->increment('age');
User::where('name', 'Jaques')->decrement('weight', 50);

$count = User->increment('age');

User::where('age', '29')->increment('age', 1, ['group' => 'thirty something']);
User::where('bmi', 30)->decrement('bmi', 1, ['category' => 'overweight']);

use Robsonvn\CouchDB\Eloquent\SoftDeletes;

class User extends Eloquent {

    use SoftDeletes;

    protected $dates = ['deleted_at'];

}

User::where('age', 'exists', true)->get();

User::where('roles', 'all', ['moderator', 'author'])->get();

User::where('tags', 'size', 3)->get();

User::where('name', 'regex', '(?i).*doe$')->get();
User::where('name', 'not regex', '(?i).*doe$')->get()

User::where('age', 'type', 2)->get();

User::where('age', 'mod', [10, 0])->get();

$user = new User;
$user->name = 'John';
$user->save();

User::create(['name' => 'John']);

$user = User::first();
$user->email = '[email protected]';
$user->save();

$user = User::first();
$user->delete();

User::destroy('517c43667db388101e00000f');

use Robsonvn\CouchDB\Eloquent\Model as Eloquent;

class User extends Eloquent {

    protected $dates = ['birthday'];

}

$users = User::where('birthday', '>', new DateTime('-18 years'))->get();

use Robsonvn\CouchDB\Eloquent\Model as Eloquent;

class User extends Eloquent {

    public function items()
    {
        return $this->hasMany('Item');
    }

}

use Robsonvn\CouchDB\Eloquent\Model as Eloquent;

class Item extends Eloquent {

    public function user()
    {
        return $this->belongsTo('User');
    }

}

use Robsonvn\CouchDB\Eloquent\Model as Eloquent;

class User extends Eloquent {

    public function groups()
    {
        return $this->belongsToMany('Group', null, 'user_ids', 'group_ids');
    }

}

use Robsonvn\CouchDB\Eloquent\Model as Eloquent;

class User extends Eloquent {

    public function books()
    {
        return $this->embedsMany('Book');
    }

}

$books = User::first()->books;

$user = $book->user;

$book = new Book(['title' => 'A Game of Thrones']);

$user = User::first();

$book = $user->books()->save($book);
// or
$book = $user->books()->create(['title' => 'A Game of Thrones'])

$book = $user->books()->first();

$book->title = 'A Game of Thrones';

$book->save();

$book = $user->books()->first();

$book->delete();
// or
$user->books()->destroy($book);

$user->books()->associate($book);

$user->save();

return $this->embedsMany('Book', 'local_key');

use Robsonvn\CouchDB\Eloquent\Model as Eloquent;

class Book extends Eloquent {

    public function author()
    {
        return $this->embedsOne('Author');
    }

}

$author = Book::first()->author;

$author = new Author(['name' => 'John Doe']);

$book = Books::first();

$author = $book->author()->save($author);
// or
$author = $book->author()->create(['name' => 'John Doe']);

$author = $book->author;

$author->name = 'Jane Doe';
$author->save();

$newAuthor = new Author(['name' => 'Jane Doe']);
$book->author()->save($newAuthor);

User::whereRaw(['age' => array('$gt' => 30, '$lt' => 40)])->get();

// Returns a collection of User models.
$models = User::raw(function($collection)
{
    return $collection->find(['_id'=>['$gt'=>null]]);
});

// Returns the original CouchDB response.
$cursor = DB::collection('users')->raw(function($collection)
{
    return $collection->find(['_id'=>['$gt'=>null]]);
});

$client = DB::getCouchDBClient();