PHP code example of friendsofcat / laravel-couchbase

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

    

friendsofcat / laravel-couchbase example snippets


FriendsOfCat\Couchbase\CouchbaseServiceProvider::class,

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

$app->withEloquent();

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

'couchbase' => [
    'driver'   => 'couchbase',
    'host'     => env('DB_HOST', 'localhost'),
    'port'     => env('DB_PORT', 8091),
    'bucket'   => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
],

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



use FriendsOfCat\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {
  $connection = 'couchbase';
}

use FriendsOfCat\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {}

use FriendsOfCat\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {

    protected $table = 'my_users';

}

use FriendsOfCat\Couchbase\Eloquent\Model as Eloquent;

class MyModel extends Eloquent {

    protected $connection = 'couchbase';

}

'CouchbaseModel'       => 'FriendsOfCat\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::whereIsMissing('updated_at')->get();

$users = User::whereIsValued('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 FriendsOfCat\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 FriendsOfCat\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {

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

}

use FriendsOfCat\Couchbase\Eloquent\Model as Eloquent;

class Item extends Eloquent {

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

}

use FriendsOfCat\Couchbase\Eloquent\Model as Eloquent;

class User extends Eloquent {

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

}

use FriendsOfCat\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 FriendsOfCat\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 FriendsOfCat\Couchbase\Eloquent\HybridRelations;

class User extends Eloquent {

    use HybridRelations;

    protected $connection = 'mysql';

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

}

use FriendsOfCat\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();

// RefreshDatabase
$this->artisan(BucketDelete::class);
$this->artisan(BucketCreate::class);
$this->artisan(BucketCreatePrimaryIndex::class);
// check hook afterApplicationCreated, beforeApplicationDestroyed
bash
php artisan couchbase:cluster:init
bash
php artisan couchbase:bucket:create --bucket canvas