You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Having been introduced to learning Laravel Framework; Over the past yr(s), Coming back to vanilla PHP,
was pretty tough. So i decided to create a much more easier way of communicating with Database, using native PHP PDO:: Driver.
use Tamedevelopers\Database\AutoLoader;
AutoLoader::start();
or -- Helpers Function
autoloader_start();
Init.php File
[optional] This will extends the composer autoload and other setup
Description
Once application is started! You can choose to include the init.php
The file includes all configuration needed and as well extends the vendor/autoload.php path.
BootLoader
If you do not want to include or use the init.php file
All you need do is call the bootloader, to start your application.
use Tamedevelopers\Database\Capsule\AppManager;
AppManager::bootLoader();
or -- Helpers Function
app_manager()->bootLoader();
Database Connection
You have the options to connect to multiple database
First navigate to [config/database.php] file and add a configuration
Takes two (2) params key as string and array [optional]
DB::connection('connName', [optional]);
Database Disconnect
If you want to connect to already connected database, You first need to disconnect
Takes one param as string
DB::disconnect('connName');
Database Reconnect
same as Database Connection
DB::reconnect('connName', [optional]);
App Debug Env
The .env file contains a key called APP_DEBUG
It's mandatory to set to false in Production environment
This helps to secure your applicaiton and exit error 404
instead of displaying entire server errors.
key
Type
Default Value
APP_DEBUG
boolean
true
More Database Connection Keys
All available connection keys
The DB_CONNECTION uses only mysql
No other connection type is supported for now.
key
Type
Default Value
driver
string
mysql
host
string
localhost
port
int
3306
database
string
username
string
password
string
charset
string
utf8mb4
collation
string
utf8mb4_unicode_ci
prefix
string
prefix_indexes
bool
false
Usage
All Methods of usage
Table
Takes a parameter as string table_name
$db->table('users');
Insert
Takes one parameter as assoc array column_name => value
It returns an object on success or error
$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
Insert Or Ignore
Same as insert() method
It returns an object of created data or false on error
Same as first() method but exit with error code 404, if data not found
$db->table('users')->firstOrFail();
Count
$db->table('users')->count();
Paginate
Takes param as int$per_page
By default if no param is given, then it displays 10 per page
$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
Similar to Laravel DB Migration Just to make database table creation more easier
object name
Returns
create()
Create table schema
run()
Begin migration
drop()
Drop migration tables
use Tamedevelopers\Database\Migrations\Migration;
Create Table Schema
Takes param as table name
Second parameter stringjobs|sessions (optional) -If passed will create a dummy jobs|sessions table schema
Migration::create('users');
Migration::create('users_wallet');
Migration::create('tb_jobs', 'jobs');
Migration::create('tb_sessions', 'sessions');
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
or -- Helpers Function
migration()->create('users');
Default String Length
In some cases you may want to setup default string legnth to all Migration Tables
Description
The Default Set is 255 But you can override by setting custom value
According to MySql v:5.0.0 Maximum allowed legnth is 4096 chars
If provided length is more than that, then we'll revert to default as the above
This affects only VACHAR
You must define this before start using the migrations
use Tamedevelopers\Database\Migrations\Schema;
Schema::defaultStringLength(200);
or -- Helpers Function
schema()->defaultStringLength(2000);
Update Column Default Value
In some cases you may want to update the default column value
Yes! It's very much possible with the help of Schema. Takes three (3) params
$tablename as string
$column_name as string
$values as mixed data NULLNOT NULL\|NoneSTRINGcurrent_timestamp()
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', []);
Run Migration
This will execute and run migrations using files located at [root/database/migrations]
Migration::run();
or
migration()->run();
Migration runned successfully on `2023_04_19_1681860618_user`
Migration runned successfully on `2023_04_19_1681860618_user_wallet`
Drop Migration
Read more...
Be careful as this will execute and drop all files table located in the migration
[optional param] bool to force delete of tables
Migration::drop();
or
migration()->drop(true);
Drop Table
Read more...
Takes one param as string $table_name
use Tamedevelopers\Database\Migrations\Schema;
Schema::dropTable('table_name');
or
schema()->dropTable('table_name');
Drop Column
Read more...
To Drop Column takes two param
This will drop the column available
use Tamedevelopers\Database\Migrations\Schema;
Schema::dropColumn('table_name', 'column_name');
or
schema()->dropColumn('table_name', 'column_name');
Get Database Config
$db->getConfig()
Get Database Connection
$db->dbConnection()
or -- Helpers Function
db_connection();
Get Database Name
$db->getDatabaseName()
Get Database PDO
$db->getPDO()
Get Database TablePrefix
$db->getTablePrefix()
Database Import
You can use this class to import .sql into a database programatically
Remember the system already have absolute path to your project.
use Tamedevelopers\Database\DBImport;
$database = new DBImport();
// needs absolute path to database file
$status = $database->import('path_to/orm.sql');
- 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
or -- Helpers Function
import('path_to/orm.sql');
Update Env Variable
You can use this class to import .sql into a database programatically
Params
Description
key
ENV key
value
ENV value
allow_quote
true | false - Default is true (Allow quotes within value)
allow_space
true | false - Default is false (Allow space between key and value)
You can as well extends the DB Model class directly from other class
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();
}
}
Helpers Functions
function name
Description
db()
Return instance of new DB($options) class
db_connection()
Same as $db->dbConnection()
config_pagination()
Same as $db->configPagination() or AutoLoader::configPagination
autoloader_start()
Same as AutoLoader::start()
env_update()
Same as Env::updateENV method
app_manager()
Return instance of (new AppManager) class
import()
Return instance of (new DBImport)->import() method