PHP code example of envor / laravel-datastore

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

    

envor / laravel-datastore example snippets


return [
    'model' => \Envor\Datastore\Models\Datastore::class,
    'create_databases' => env('DATASTORE_CREATE_DATABASES', true),
];

use Envor\Datastore\DatabaseFactory;

$sqlite = DatabaseFactory::newDatabase('mydb', 'sqlite');

// Envor\Datastore\Databases\SQLite {#2841 ...

$sqlite->create();

// true

$sqlite->name;

// ...storage/app/datastore/mydb.sqlite

$sqlite->connection;

// mydb

$sqlite->migrate();

  //  INFO  Preparing database.  

  // Creating migration table ................ 9.55ms DONE

$sqlite->configure();


config('database.default');

// "mydb"

config('database.connections.mydb');

// [
//     "driver" => "sqlite",
//     "url" => null,
//     "database" => "...storage/app/datastore/mydb.sqlite",
//     "prefix" => "",
//     "foreign_key_constraints" => true,
//     "name" => "mydb",
// ]

    /**
     * Create a newly registered user.
     *
     * @param  array<string, string>  $input
     */
    public function create(array $input): User
    {
        $create = function () use ($input) {
            Validator::make($input, [
                'name' => ['();

            return DB::transaction(function () use ($input) {
                return tap(User::create([
                    'name' => $input['name'],
                    'email' => $input['email'],
                    'password' => Hash::make($input['password']),
                ]), function (User $user) {
                    $this->createTeam($user);
                });
            });
        };

        SQLite::make(database_path('my_backup.sqlite'))
            ->create()
            ->migratePath('database/migrations/platform')
            ->migrate()
            ->run($create)
            ->disconnect();
            
        MariaDB::make('backup')
            ->create()
            ->migratePath('database/migrations/platform')
            ->migrate()
            ->run($create)
            ->disconnect();

        return MariaDB::make('datastore')
            ->create()
            ->migratePath('database/migrations/platform')
            ->migrate()
            ->run($create)
            ->return();
    }


Route::get('/contexed', fn() => 'OK')->middleware('datastore.context');

// or
Route::get('/contexed', fn() => 'OK')->middleware(\Envor\Datastore\DatastoreContextMiddleware::class);

// will use the authenticated user to configure a database


...
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements \Envor\Datastore\Contracts\HasDatastoreContex
{
    use \Envor\Datastore\Concerns\BelongsToDatastore

    public function datastoreContext(): \Envor\Datastore\Contracts\ConfiguresDatastore;
    {
        return $this->datastore;
    }
}


interface HasDatastoreContext
{
    public function datastoreContext(): ?\Envor\Datastore\Contracts\ConfiguresDatastore;
}


interface ConfiguresDatastore
{
    public function configure();

    public function use();

    public function database(): ?\Envor\Datastore\Datastore;
}
bash
php artisan vendor:publish --tag="datastore-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="datastore-config"