PHP code example of mpociot / couchbase

1. Go to this page and download the library: Download mpociot/couchbase 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/ */

    

mpociot / couchbase example snippets


Mpociot\Couchbase\CouchbaseServiceProvider::class,

$app->register(Mpociot\Couchbase\CouchbaseServiceProvider::class);

$app->withEloquent();

$capsule->getDatabaseManager()->extend('couchbase', function($config)
{
    return new Mpociot\Couchbase\Connection($config);
});

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

'couchbase' => [
    'driver'   => 'couchbase',
    'host'     => env('DB_HOST', 'localhost'),
    'port'     => env('DB_PORT', 8091),
    'bucket'   => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
    'n1ql_hosts' => [
        'http://'.env('DB_HOST', 'localhost').':8093'
    ]
],

'couchbase' => [
    'driver'   => 'couchbase',
    'host'     => ['host1', 'host2'],
    'port'     => env('DB_PORT', 8091),
    'bucket'   => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
    'n1ql_hosts' => [
        'http://host1:8093',
        'http://host2:8093'
    ]
],

use Mpociot\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {}

use Mpociot\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {

    protected $table = 'my_users';

}

use Mpociot\Couchbase\Eloquent\Model as Eloquent;

class MyModel extends Eloquent {

    protected $connection = 'couchbase';

}

'CouchbaseModel'       => 'Mpociot\Couchbase\Eloquent\Model',

class MyModel extends CouchbaseModel {}

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

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

$user = DB::connection('couchbase')->table('users')->get();

$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::distinct()->get(['name']);
// or
$users = User::distinct('name')->get();

$users = User::where('active', true)->distinct('name')->get();

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

$users = Users::groupBy('title')->get(['title', 'name']);

$total = Order::count();
$price = Order::max('price');
$price = Order::min('price');
$price = Order::avg('price');
$total = Order::sum('price');

$sold = Orders::where('sold', true)->sum('price');

$user = Comment::where('body', 'like', '%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 Mpociot\Couchbase\Eloquent\SoftDeletes;

class User extends Eloquent {

    use SoftDeletes;

    protected $dates = ['deleted_at'];

}

$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 Mpociot\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {

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

}

use Mpociot\Couchbase\Eloquent\Model as Eloquent;

class Item extends Eloquent {

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

}

use Mpociot\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {

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

}

use Mpociot\Couchbase\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 Mpociot\Couchbase\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);

use Mpociot\Couchbase\Eloquent\HybridRelations;

class User extends Eloquent {

    use HybridRelations;

    protected $connection = 'mysql';

    public function messages()
    {
        return $this->hasMany('Message');
    }

}

use Mpociot\Couchbase\Eloquent\Model as Eloquent;

class Message extends Eloquent {

    protected $connection = 'couchbase';

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

}

$users = User::remember(10)->get();

DB::connection()->disableQueryLog();