<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
avadim / manticore-query-builder-laravel example snippets
// the short name of the facade - the \ManticoreDb that Laravel registers by itself
$app->withFacades(true, [
'avadim\Manticore\Laravel\Facade' => 'ManticoreDb',
]);
// Register Config Files
$app->configure('manticore');
// Register Service Providers
$app->register(avadim\Manticore\Laravel\ServiceProvider::class);
// Get list of tables via the default connection
$list = \ManticoreDb::showTables();
// Get list of tables via the specified connection
$list = \ManticoreDb::connection('test')->showTables();
\ManticoreDb::table('t')->insert($data);
\ManticoreDb::table('t')->match($match)->where($where)->get();
use avadim\Manticore\Laravel\Facade as ManticoreDb;
use avadim\Manticore\Laravel\Manager;
// the facade, when a global alias is not to your taste
ManticoreDb::table('products')->find($id);
// injected, when a class is to be testable without the framework around it
class ProductSearch
{
private $manticore;
public function __construct(Manager $manticore)
{
$this->manticore = $manticore;
}
public function find(string $text)
{
return $this->manticore->table('products')->match($text)->get();
}
}
// Illuminate\Support\Collection, keyed from zero
$rows = \ManticoreDb::table('products')->match('galaxy')->get();
$titles = $rows->map(fn ($row) => $row->title)->all();
// A single row is a Row object
$row = \ManticoreDb::table('products')->where('price', '<', 1000)->first();
$row = \ManticoreDb::table('products')->find($id);
// pluck() answers with a Collection too, keyed by the second column when it is given
$titles = \ManticoreDb::table('products')->pluck('title');
$titles = \ManticoreDb::table('products')->pluck('title', 'id');
$row->title; // as an object, like in Laravel
$row['title']; // as an array, like in the standalone builder
$row->toArray();
$row->toJson();
json_encode($rows); // an array of objects, as expected of a JSON API
// Illuminate\Pagination\LengthAwarePaginator, the page number taken from the request
$products = \ManticoreDb::table('products')->match('galaxy')->paginate(15);
// ... and without the COUNT(*) of the total, when the template only needs "next" and "previous"
$products = \ManticoreDb::table('products')->simplePaginate(15);
\ManticoreDb::transaction(function ($connection) {
$connection->table('products')->insert($data);
$connection->table('log')->insert($record);
});
// ... or by hand
$connection = \ManticoreDb::connection();
$connection->beginTransaction();
$connection->table('products')->insert($data);
$connection->commit(); // or rollBack()
\ManticoreDb::connection()->statement('FLUSH RAMCHUNK products'); // true when the server accepted it
use avadim\Manticore\QueryBuilder\Schema\SchemaTable;
class CreateManticoreProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\ManticoreDb::create('products', function (SchemaTable $table) {
$table->timestamp('created_at');
$table->string('name');
$table->text('description');
$table->float('price');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
\ManticoreDb::drop('products', true); // true - drop it only if it exists
}
}
class AddRatingToManticoreProducts extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\ManticoreDb::addColumn('products', 'rating', 'int');
\ManticoreDb::addColumn('products', 'summary', 'text');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
\ManticoreDb::dropColumn('products', 'rating');
\ManticoreDb::dropColumn('products', 'summary');
}
}
\ManticoreDb::forgetSchema();
// Enable logging for all
\ManticoreDb::setLogger(\Log::getLogger());
// Enable logging for the specified connection
\ManticoreDb::connection('test')->setLogger(\Log::getLogger());
// Enable logging for the next query
\ManticoreDb::table('test')->match($match)->where($where)->setLogger(\Log::getLogger())->get();
sh
php artisan vendor:publish --provider="avadim\Manticore\Laravel\ServiceProvider"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.