PHP code example of tamedevelopers / database

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

    

tamedevelopers / database example snippets




use Tamedevelopers\Database\AutoLoader;

AutoLoader::start();
// then reload your browser to allow the system scalfold for you

use Tamedevelopers\Support\Capsule\Artisan;

Artisan::call('db:wipe --force');

use Tamedevelopers\Database\Capsule\AppManager;

AppManager::bootLoader();
// app_manager()->bootLoader();

DB::connection('connName', $options);

DB::disconnect('connName');

DB::reconnect('connName', $options);

$db = DB::connection();

$db->table('users');

DB::table('users')->insert([
    'user_id'    => 10000001,
    'first_name' => 'Alfred',
    'last_name'  => 'Pete',
    'wallet_bal' => 0.00,
    'registered' => strtotime('now'),
]);

-- To see data, you need to save into a variable

DB::table('users')->insertOrIgnore([
    'user_id'    => 10000001,
    'first_name' => 'Alfred',
]);

DB::table('users')
    ->where('user_id', 10000001)
    ->update([
        'first_name' => 'Alfred C.',
    ]);

DB::table('users')
    ->where('user_id', 10000001)
    ->updateOrIgnore([
        'first_name' => 'Alfred C.',
    ]);

DB::table('users')
    ->where('user_id', 10000001)
    ->delete();

DB::table('posts')->destroy(1);
// Query: delete from `posts` where `id` = ?

DB::table('posts')->destroy(10, 'post_id');
// Query: delete from `posts` where `post_id` = ?

DB::table('users')
    ->where('user_id', 10000001)
    ->increment('wallet_bal');

DB::table('users')
    ->where('user_id', 10000001)
    ->increment('wallet_bal', 10);

DB::table('users')
    ->where('user_id', 10000001)
    ->increment('wallet_bal', 100.23, [
        'first_name' => 'F. Peterson',
        'status'     => 1,
    ]);

DB::table('users')
    ->where('user_id', 10000001)
    ->increment('wallet_bal', [
        'first_name' => 'F. Peterson',
        'status'     => 1,
    ]);

DB::table('users')
    ->where('user_id', 10000001)
    ->decrement('wallet_bal', [
        'first_name' => 'F. Peterson',
        'status'     => 1,
    ]);

DB::table('blog')->min('amount');

DB::table('blog')->max('amount');

DB::table('blog')->sum('amount');

DB::table('blog')->avg('amount');
DB::table('blog')->average('amount');

DB::table('users')->get();

DB::table('users')->first();

DB::table('users')->firstOrCreate(
    ['email' => 'example.com']
);

DB::table('users')->firstOrCreate(
    ['email' => 'example.com'],
    [
        'country'   => 'Nigeria',
        'age'       => 18,
        'dob'       => 2001,
    ]
);

DB::table('users')->firstOrFail();

DB::table('users')->count();

$users = DB::table('users')
            ->paginate(40);

$users // this will return the data objects
$users->links() // this will return the paginations links view
$users->showing() // Display items of total results

DB::table('users')
    ->where('email', '[email protected]')
    ->orWhere('name', 'Mandison')
    ->exists();

DB::tableExists('users');

$user = DB::tableExists('users')
            ->first();

if($user){
    $user->first_name
    $user['first_name']
}

$user->toArray()
$user->getAttributes()

$users = DB::tableExists('users')
            ->where('is_active', 1),
            ->random(),
            ->get();

if($users->isNotEmpty()){
    foreach($users as $user){
        $user->first_name
        $user['first_name']
        $user->toArray()
        $user->getAttributes()
    }
}

use Tamedevelopers\Database\Auth;

$admin = Auth::guard('admins');

use Tamedevelopers\Database\Auth;

// Create guards
$admin = (new Auth)->guard('tb_admin');
$user  = (new Auth)->guard('tb_user', 'woocommerce');

// Credentials (password is ser->attempt($credentials)) {
    // In-memory user available
    $user->check();          // true
    $user->id();             // e.g., 123
    $user->user();           // full user array
}

// 2) Persist explicitly (similar to Laravel Auth::login())
$user->login($user->user());   // stores sanitized user in session (no password)

// 3) Retrieve later in another request
$another = (new Auth)->guard('tb_user', 'woocommerce');
$another->user();    // rehydrated from session
$another->check();   // true if session had user

// 4) Logout
$another->logout();  // clears in-memory and session

AutoLoader::configPagination([
    'allow' => true, 
    'prev'  => 'Prev Page', 
    'last'  => 'Last Page', 
    'next'  => 'Next Page', 
    'view'  => 'bootstrap',
    'class' => 'Custom-Class-Css-Selector', 
]);

$users = DB::table('users')->paginate(40);

$users
// This will return `Collections` of pagination data

$users->links();
// This will return pagination links view

$users->links([
    'first' => 'First Page',
    'last'  => 'Last Page',
    'prev'  => 'Previous Page',
    'next'  => 'Next Page',
    'no_content'  => 'All videos has been loaded',
])

$users->showing();

// This will create a span html element with text
<span class='page-span'>
    Showing 0-40 of 500 results
</span>

$users->showing([
    'showing'  => 'Showing',
    'of'       => 'out of',
    'results'  => 'Results',
    'span'     => 'css-selector',
])

$users = DB::table('users')->paginate(20);

foreach($users as $user){
    echo $user->numbers();
}

$users = DB::table('users')->paginate(20);

$users = DB::table('users')->paginate(20);

$users->getPagination();

DB::query("SHOW COLUMNS FROM users")
    ->limit(10)
    ->get();


DB::query("ALTER TABLE `langs` ADD COLUMN es TEXT; UPDATE `langs` SET es = en;")
    ->exec();

DB::table('users')
    ->where('user_id', 10000001)
    ->select(['first_name', 'email'])
    ->select('email', 'name')
    ->first();

DB::table('wallet')
    ->orderBy('date', 'DESC')
    ->get();

DB::table('wallet')
    ->orderByRaw('CAST(`amount` AS UNSIGNED) DESC')
    ->get();

DB::table('wallet')
    ->latest('date')
    ->get();

DB::table('wallet')
    ->oldest()
    ->get();

DB::table('wallet')
    ->inRandomOrder()
    ->get();

DB::table('wallet')
    ->random()
    ->get();

DB::table('wallet')
    ->limit(10)
    ->get();

DB::table('wallet')
    ->limit(3)
    ->offset(2)
    ->get();

DB::table('wallet')
    ->offset(2)
    ->get();

DB::table('wallet')
    ->join('users', 'users.user_id', '=', 'wallet.user_id')
    ->get();

DB::table('wallet')
    ->join('users', 'users.user_id', '=', 'wallet.user_id')
    ->where('wallet.email', 'example.com')
    ->orWhere('wallet.user_id', 10000001)
    ->paginate(10);

DB::table('wallet')
    ->leftJoin('users', 'users.user_id', '=', 'wallet.user_id')
    ->where('wallet.email', 'example.com')
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->where('amount', '>', 10)
    ->where('balance', '>=', 100)
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->where('amount', '>', 10)
    ->orWhere('first_name', 'like', '%Peterson%')
    ->where('amount', '<=', 10)
    ->get();

$date = strtotime('next week');

DB::table("tb_wallet")
    ->whereRaw("NOW() > created_at")
    ->whereRaw("date >= ?", [$date])
    ->where(DB::raw("YEAR(created_at) = 2022"))
    ->where('email', '[email protected]')
    ->limit(10)
    ->random()
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->whereColumn('amount', 'tax')
    ->whereColumn('amount', '<=', 'balance')
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->whereNull('email_status')
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->whereNotNull('email_status')
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->whereBetween('amount', [0, 100])
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->whereNotBetween('amount', [0, 100])
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->whereIn('amount', [10, 20, 40, 100])
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->whereNotIn('amount', [10, 20, 40, 100])
    ->get();

DB::table('wallet')
    ->where('user_id', 10000001)
    ->groupBy('amount')
    ->get();

use Tamedevelopers\Database\Migrations\Migration;

Migration::create('users');
Migration::create('users_wallet');
Migration::create('tb_jobs', 'jobs');
Migration::create('tb_sessions', 'sessions'); 
// migration()->create('users');

// Table `2023_04_19_1681860618_user` has been created successfully
// Table `2023_04_19_1681860618_user_wallet` has been created successfully
// Table `2023_04_19_1681860618_tb_jobs` has been created successfully
// Table `2023_04_19_1681860618_tb_sessions` has been created successfully

use Tamedevelopers\Database\Migrations\Schema;

Schema::defaultStringLength(200);
// schema()->defaultStringLength(2000);

use Tamedevelopers\Database\Migrations\Schema;

Schema::updateColumnDefaultValue('users_table', 'email_column', 'NOT NULL');
Schema::updateColumnDefaultValue('users_table', 'gender_column', []);

// or
// schema()->updateColumnDefaultValue('users_table', 'gender_column', []);

Migration::run();

or
migration()->run();

// Migration runned successfully on `2023_04_19_1681860618_user` 
// Migration runned successfully on `2023_04_19_1681860618_user_wallet` 

Migration::drop();

or
migration()->drop(true);

use Tamedevelopers\Database\Migrations\Schema;

Schema::dropTable('table_name');

or 
schema()->dropTable('table_name');

use Tamedevelopers\Database\Migrations\Schema;

Schema::dropColumn('table_name', 'column_name');

or 
schema()->dropColumn('table_name', 'column_name');

$db->getConfig()

$db->dbConnection()

$db->getDatabaseName()

$db->getPDO()

$db->getTablePrefix()

use Tamedevelopers\Database\DBImport;

$database = new DBImport('path_to/orm.sql', 'connName');
// new DBImport(base_path('path_to/orm.sql'))

// run the method
$status = $database->run();

// - Status code
// ->status == 404 (Failed to read file or File does'nt exists
// ->status == 400 (Query to database error
// ->status == 200 (Success importing to database

use Tamedevelopers\Support\Env;

Env::updateENV('DB_PASSWORD', 'newPassword');
Env::updateENV('APP_DEBUG', false);
Env::updateENV('DB_CHARSET', 'utf8', false);

// env_update('DB_CHARSET', 'utf8', false);
// Returns - Boolean
// true|false

use Tamedevelopers\Database\Model;

class Post extends Model{
    
    // define your custom model table name
    protected $table = 'posts';

    // -- You now have access to the DB public instances
    public function getPost(){
        return $this->select(['images', 'title', 'description'])->get();
    }
}
bash
php tame list
bash
php tame scaffold:run --force
html
<div data-pagination-content>
    <div class="wallet-container" data-pagination-append>
         foreach($users as $user) {