1. Go to this page and download the library: Download colopl/laravel-spanner 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/ */
// `default_sequence_kind` must be set in order to use auto increment
$schemaBuilder->setDatabaseOptions([
'default_sequence_kind' => 'bit_reversed_positive',
]);
$schemaBuilder->create('user', function (Blueprint $table) {
$table->integer('id')->primary()->autoIncrement();
});
// BAD: Do not use transactions manually!!
try {
DB::beginTransaction();
...
DB::commit();
} catch (\Throwable $ex) {
DB::rollBack();
}
// GOOD: You should always use transaction method
DB::transaction(function() {
...
});
// There are four types of timestamp bounds: ExactStaleness, MaxStaleness, MinReadTimestamp and ReadTimestamp.
$timestampBound = new ExactStaleness(10);
// by Connection
$connection->selectWithTimestampBound('SELECT ...', $bindings, $timestampBound);
// by Query Builder
$queryBuilder
->withStaleness($timestampBound)
->get();
$timestampBound = new ExactStaleness(10);
// by Connection
$connection->snapshot($timestampBound, function() use ($connection) {
$result1 = $connection->table('foo')->get();
$result2 = $connection->table('bar')->get();
return [$result1, $result2];
);
// by Model
User::where('foo', 'bar')
->snapshot($timestampBound)
->get();
// by Query Builder
$queryBuilder
->snapshot($timestampBound)
->get();
// Using Connection
$connection->selectWithOptions('SELECT ...', $bindings, ['dataBoostEnabled' => true]);
// Using Query Builder
$queryBuilder
->useDataBoost()
->setRequestTimeoutSeconds(60)
->get();
$schemaBuilder->create('user', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
// adds an invisible column for full text search
$table->tokenList('UserNameTokens', TokenizerFunction::FullText, 'name', ['language_tag' => 'en']);
// adds a SEARCH INDEX
$table->fullText(['UserNameTokens']);
});
User::query()->searchFullText('UserNameTokens', 'John OR Kevin', ['enhance_query' => true])->get();
use Colopl\Spanner\Schema\QueueDefinition;
$schemaBuilder->createQueue('Jobs', function (Blueprint $queue, QueueDefinition $definition) {
// Collect messages without delivering them yet.
$definition->disableDelivery(true);
$queue->string('MessageId', 36)->primary();
$queue->string('Payload', 'max');
});
$schemaBuilder->table('Jobs', function (Blueprint $queue) {
$queue->alterQueue()->disableDelivery(true);
});
// Passing null resets an option back to its default.
$schemaBuilder->table('Jobs', function (Blueprint $queue) {
$queue->alterQueue()->disableDelivery(null);
});
[
'connections' => [
'spanner' => [
'driver' => 'spanner',
// Database connection from config/database.php. Defaults to the default connection.
'connection' => 'spanner',
// Name of the Spanner queue, as created above.
'queue' => 'Jobs',
// Seconds a reserved job stays invisible before it is delivered again.
'retry_after' => 90,
// Seconds a single receiving call waits for messages.
'block_for' => 20,
'after_commit' => false,
],
],
];
class ProcessUserTask implements ShouldQueue, ProvidesInterleaveKeys
{
public function __construct(private User $user) {}
public function getInterleaveKeyValues(): array
{
return ['UserId' => $this->user->getKey()];
}
}
DB::connection('spanner')->transaction(function () use ($user) {
$user->save();
// Never delivered if the transaction is rolled back.
ProcessSignup::dispatch($user);
});