PHP code example of clusterpoint / laravel-clusterpoint

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

    

clusterpoint / laravel-clusterpoint example snippets




namespace App\Http\Controllers;

use Clusterpoint\Client; // use our package.

class ExampleController extends Controller
{
    public function getIndex() {
		
		$cp = new Client(); // by defualt uses 'default' connection name from ./config/clusterpoint.php
		
		// Set the collection to work with to initalize the query builder for it.
		$collection = $cp->database("database.collection");

		// Build your query
		$results = $collection->where('color', 'red')
			->where('availability', true)
			->limit(5)
			->groupBy('category')
			->orderBy('price')
			->select(['name', 'color', 'price', 'category'])
			->get();
			
		// Access your results
		return $results[0]->price;
	}
}



namespace App;

use Clusterpoint\Model;

class Example extends Model
{
	protected $db = "database.collection"; // set your databse and collection names
	//protected $primaryKey = "custom_id"; // If you want to define specific specific primary key, default = _id
}



namespace App\Http\Controllers;

use App\Example; // use your model.

class ExampleController extends Controller
{	
    public function getIndex() {
		$example = Example::where('price', '>', 200)->first();
		
		$example->price = 300;
		$example->save();
		
		return view('example', compact('example'));
	}
}



Route::model('example', 'App\Example');
Route::get('/examples/{example}', 'ExampleController@getIndex');



namespace App\Http\Controllers;

use App\Example;

class ExampleController extends Controller
{
	public function getIndex($example) {
		$id = $example->_id; // value is 42
		$name = $example->name;
		$price = $example->price;
		return view('example', compact('name','price'));
	}
}



return array(
  "default" => array(
    'host' => env('CP_HOST', 'https://api-eu.clusterpoint.com/v4'),
    'account_id' => env('CP_ID', ''),
    'username' => env('CP_USERNAME', ''),
    'password' => env('CP_PASSWORD', ''),
  ),
  "test" => array(
    'host' => env('CP1_HOST', 'https://api-eu.clusterpoint.com/v4'),
    'account_id' => env('CP1_ID', ''),
    'username' => env('CP1_USERNAME', ''),
    'password' => env('CP1_PASSWORD', ''),
  )
);




namespace App;

use Clusterpoint\Model;

class Example extends Model
{
	protected $connection = "test";
}