PHP code example of julien-c / mongovel

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

    

julien-c / mongovel example snippets


class Book extends MongovelModel
{
}

public function show($id)
{
	$book = Book::findOne(new MongoId($id));

	// Here, you can access the book's attributes like in Eloquent:
	// $book->title, $book->reviews, etc.
	// $book->id is a string representation of the object's MongoId.

	// Let's say we're an API, so let's just send the object as JSON:

	return $book;
}

public function store()
{
	$book = Input::only('title', 'content');

	Book::insert($book);
}

public function reviewStore($id)
{
	$review = Input::all();

	// You can leverage the full power of Mongo query operators:
	Book::update(new MongoId($id),
		array('$push' => array('reviews' => $reviews))
	);

	return Response::json(array('status' => 201), 201);
}

public function destroy($id)
{
	Book::remove(new MongoId($id));
}

public function index()
{
	$books = Book::find();

	$books->each(function($book) {
		// Do anything you would do on a Laravel Collection
	});

	return $books;
}

$bulk = Book::initializeOrderedBulk();

$bulk->insert(array('author' => 'Me', 'title' => 'My life'));

$bulk->find(array('author' => 'Me'))
     ->update(array('$push' => array('reviews' => "Awesome!")));

$bulk->find(array('title' => 'My life'))
     ->updateOne(array('$push' => array('reviews' => "Incredible!")));

$bulk->find(array('title' => 'My life, II'))
     ->upsert()
     ->update(array('$push' => array('reviews' => "Can't wait!")));

$bulk->find(array('author' => 'Not me'))
     ->remove();

$bulk->find(array('title' => 'My life, II'))
     ->removeOne();

$bulk->execute();

'Mongovel\MongovelServiceProvider'

'MongovelModel' => 'Mongovel\Model'

'mongodb' => array(
	'default' => array(
		'host'     => 'localhost',
		'port'     => 27017,
		'database' => 'laravel',
	)
)