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/ */
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');
}
}
$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');
}
}