Download the PHP package tamedevelopers/database without Composer
On this page you can find all versions of the php package tamedevelopers/database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tamedevelopers/database
More information about tamedevelopers/database
Files in tamedevelopers/database
Package database
Short Description Lightweight PHP ORM Database Model.
License MIT
Homepage https://github.com/tamedevelopers/database
Informations about the package database
PHP ORM Database
Inspiration
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.
Documentation
- Requirements
- Installation
- Instantiate
- Init php
- Tame Cli
- Tame Cli Scaffold
- Tame Cli DB
- Tame Cli Migrate
- Tame Cli Make
- Tame Cli Key
- Tame Artisan Call
- BootLoader
- Database Connection
- Database Disconnect
- App Debug ENV
- Usage
- table
- from
- insert
- insertOrIgnore
- update
- updateOrIgnore
- destroy
- delete
- increment
- decrement
- min
- max
- sum
- avg
- average
- Database Connection Keys
- Fetching Data
- get
- first
- firstOrCreate
- firstOrFail
- count
- paginate
- exists
- doesntExist
- tableExists
- Collections
- Collection Methods
- Collection Usage
- Auth
- Auth Methods
- Auth Usage
- guard
- attempt
- login
- user
- logout
- id
- Pagination
- Global Configuration
- Pagination Query
- Pagination Data
- Pagination Links
- Pagination Links Config
- Pagination Showing
- Pagination Showing Config
- Pagination Foreach Numbers
- Pagination Ajax Loading
- Get Pagination
- Clause
- query
- select
- selectRaw
- orderBy
- orderByRaw
- orderByDesc
- orderByAsc
- latest
- oldest
- inRandomOrder
- random
- limit
- take
- offset
- join
- joinWhere
- leftJoin
- leftJoinWhere
- rightJoin
- rightJoinWhere
- crossJoin
- where
- orWhere
- whereNot
- orWhereNot
- whereRaw
- whereColumn
- orWhereColumn
- whereNull
- orWhereNull
- whereNotNull
- orWhereNotNull
- whereBetween
- orWhereBetween
- whereNotBetween
- orWhereNotBetween
- whereBetweenColumns
- orWhereBetweenColumns
- whereNotBetweenColumns
- orWhereNotBetweenColumns
- whereDate
- orWhereDate
- whereTime
- orWhereTime
- whereDay
- orWhereDay
- whereMonth
- orWhereMonth
- whereYear
- orWhereYear
- having
- orHaving
- havingNull
- orHavingNull
- havingNotNull
- orHavingNotNull
- havingBetween
- havingRaw
- whereIn
- orWhereIn
- whereNotIn
- orWhereNotIn
- orHavingRaw
- groupBy
- groupByRaw
- Database Migration
- Create Table Schema
- Default String Length
- Update Column Default Value
- Run Migration
- Drop Migration
- dropTable
- dropColumn
- Get Database Config
- Get Database Connection
- Get Database Name
- Get Database PDO
- Get Database TablePrefix
- Database Import
- Update Env Variable
- Autoload Register
- Collation And Charset
- Extend Model Class
- Helpers Functions
- Error Dump
- Error Status
- Useful links
Requirements
>= php 8.0+
Installation
Prior to installing database package get the Composer dependency manager for PHP because it'll simplify installation.
Instantiate
Step 1 — Require composer autoload:
Step 2 — [optional] If you want the Package scalfolding
- This will auto setup your entire application on a
go!- It's helper class can be called, using --
autoloader_start()
- It's helper class can be called, using --
| Description |
|---|
| It's important to install vendor in your project root, As we use this to get your root [dir] |
| By default you don't need to define any path again |
| Files you'll see after you reload browser: |
.env .env.example .gitignore .htaccess .user.ini init.php |
init php
- [optional] This will extends the
composer autoloadand other setup- If you used the package
Package scalfoldingthis file will be automatically generated, that you can include at the beginning of your project.
- If you used the package
Tame Cli
- Custom commands support for CLI
tame-cli-scaffold
- Scalffold the database files, instead of running
AutoLoader::start()in browser.- The
-f|--forceflag is needed if you're inside a framework or if on production server. - This doesn't alter nor replace any files that exists and safe.
- The
tame-artisan-call
- Using the CLI from within php, without the CMD interface.
BootLoader
- [optional] from
version ^6.0.3If you do not want to include or use theinit.phpfile- You can as well call the bootloader, to start the database life-circle.
Database Connection
- Take two param as
[$name|$options]- Mandatory
$nameas string of connection name - [optional]
$optionsand an array, if no connection data is found - First navigate to [config/database.php] file and add connection configuration or use .env
- Mandatory
Database Disconnect
- If you want to connect to already connected database, You first need to disconnect
- Takes one param as
string
- Takes one param as
Database Reconnect
- same as
Database Connection
App Debug Env
- The
.envfile contains a key calledAPP_DEBUG- It's mandatory to set to false on Production environment
- This helps to secure your applicaiton and exit with error 404
- instead of displaying entire server errors.
| key | Type | Default Value |
|---|---|---|
| APP_DEBUG | boolean | true |
Database Connection Keys
- All available connection keys
- The DB_CONNECTION uses only
mysql - No other connection type is supported for now.
- The DB_CONNECTION uses only
| 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
- Without calling the
DB::connection()and passing the driver name you want. It will automatically be using the default connection driver, you've in your setup'
- Without calling the
Table
- Takes a parameter as
stringtable_name
Insert
- Takes one parameter as assoc array
column_name => value- It returns an object on success or error
Insert Or Ignore
- Same as
insert()method- It returns an object of created data or
falseon error
- It returns an object of created data or
Update
- Takes one parameter as assoc array
column_name => value- Returns an
intnumbers of affected rows or error
- Returns an
Update Or Ignore
- Same as
update()method- Returns an
intnumbers of affected rows or0on error
- Returns an
delete
- Returns an
int
destroy
- Take two param as
[value|column]- Mandatory
valueas mixed value - [optional]
columnas Default isid - Returns an
int
- Mandatory
Increment
- Takes three parameter
- Only the first param is required
| param | Data types |
|---|---|
column required |
string |
count or [] |
int | array |
| param | array |
1 By default if the the second param not passed, this will increment by 1
-
You can also pass in a second or third parameter to update additional columns
- You can ommit the second param and it'll be automatically seen as update param (If an array)
Decrement
- Same as Increment
min
- Take one param as
Expression|string
max
- Same as min
sum
- Take one param as
Expression|string
avg
- Take one param as
Expression|string
Fetching Data
| object name | Returns |
|---|---|
| get() | array of objects |
| find() | object | null |
| first() | object | null |
| FirstOrIgnore() | object | null |
| FirstOrCreate() | object |
| firstOrFail() | object or exit with 404 status |
| count() | int |
| paginate() | array of objects |
| exists() | boolean true | false |
| tableExists() | boolean true | false |
GET
First
First or Create
-
Take two param as an
array- Mandatory
$conditionsparam asarray - [optional]
$dataparam asarray
- Mandatory
-
First it checks if codition to retrieve data. If fails, then it merge the
$conditionsto$datavalue to create new records - or --
Example 2
First or Fail
- Same as
first()method but exit with error code 404, if data not found
Count
Paginate
- Takes param as
int$per_page- By default if no param is given, then it displays 10 per page
Exists
- Returns boolean
true \| false
Table Exists
- Takes param as
string$table_name
Collections
- You can directly use
methodsofCollections Instanceon any of the below - All the below
methodsare received by Collectionclass- get()
- find()
- first()
- firstOrIgnore()
- firstOrCreate()
- firstOrFail()
- insert()
- insertOrIgnore()
Collection Methods
| Methods | Description |
|---|---|
| getAttributes() | array Returns an array of data |
| getOriginal() | object Returns an object of data |
| isEmpty() | boolean true \| false If data is empty |
| isNotEmpty() | opposite of ->isEmpty() |
| count() | int count data in items collection |
| toArray() | array Convert items to array |
| toObject() | object Convert items to object |
| toJson() | string Convert items to json |
Collection Usage
-
Colections are called automatically on all Database Fetch Request
- With this you can access data as an
object\|arraykey property - If no data found then it returns null on
->first()method only
- With this you can access data as an
- Example two(2)
->get() \| ->paginate()Request
Auth
- Lightweight guard-based authentication similar to Laravel.
- attempt() only validates and sets in-memory user; call login() to persist to session.
| method name | Description |
|---|---|
| guard() | Create a guard bound to a table and [optional] connection. |
| attempt() | Validate credentials, set in-memory user on success; does not persist to session. |
| login() | Persist the current user (or provided array) to session. If userData is not an array, it’s ignored. |
| user() | Get the in-memory user or rehydrate from session if available. |
| id() | Get the authenticated user’s id (or custom key). |
| logout() | Clear in-memory user and remove from session. |
auth-guard
- Set the authentication guard (Takes two param)
- Mandatory
$tableparam asstring - [optional]
$connectionparam asstring | nulldatabase connection name.
- Mandatory
Auth Usage
Pagination
- Configuring Pagination
- It's helper class can be called, using --
config_pagination()
- It's helper class can be called, using --
| key | Data Type | Description |
|---|---|---|
| allow | true | false |
Default false Setting to true will allow the system use this settings across app |
| class | string | Css selector For pagination ul tag in the browser |
| span | string | Default .page-span Css selector For pagination Showing Span tags in the browser |
| view | bootstrap | cursor | loading | onloading |
Default simple - For pagination design |
| first | string | Change the letter First |
| last | string | Change the letter Last |
| next | string | Change the letter Next |
| prev | string | Change the letter Prev |
| showing | string | Change the letter Showing |
| of | string | Change the letter of |
| results | string | Change the letter results |
| buttons | int | Numbers of pagination links to generate. Default is 5 and limit is 20 |
Global Configuration
- 1 Setup global pagination on ENV autostart
most preferredmethod
Pagination Query
Pagination Data
Pagination Links
Pagination Links Config
Read more...
- You can directly configure pagination links - It'll override the global settingsPagination Showing
Pagination Showing Config
Read more...
- You can configure showing text directly as wellPagination Foreach Numbers
- Page numbering
starts counting from 1- This will format all pagination items collections
- On each page, it starts counting from last pagination item number
Pagination Ajax Loading
- When the view is either
loading||onloading|cursor|bootstrap- This can automatically fetched data without page load
- You need to give your DOM-element
data-pagination-contentanddata-pagination-append
Get Pagination
- Returns pagination informations
| key | Description |
|---|---|
| limit | Pagination limit int |
| offset | Pagination offset int |
| page | Pagination Current page int |
| pageCount | Pagination Total page count int |
| perPage | Pagination per page count int |
| totalCount | Pagination total items count int |
Clause
- Multiple clause
Query
- Allows the use direct sql query
SQL query syntax- Or direct query exec()
- [important] you cannot use paginate on
query()method
Select
- Used to select needed columns from database
orderBy
- Takes two param
$columnand$direction- By default
$directionparam is set toASC
- By default
orderByRaw
- Takes one param
$query
Latest
- Takes one param
$columnby default the column used isid
Oldest
- Takes one param
$columnby default the column used isid
inRandomOrder
random
Read more...
- Same as `inRandomOrder()`limit
- Takes one param
$limitas int. By default value is1
offset
Read more...
- Takes one param `$offset` as int. By default value is `0` - Example 2 (Providing only offset will return as LIMIT without error)join
- Includes
join|leftJoin|rightJoin|crossJoin
| Params | Description |
|---|---|
| table | table |
| foreignColumn | table.column |
| operator | operator sign |
| localColumn | local_table.column |
- or
leftJoin
- Same as
join
where
- Takes three parameter
- Only the first param is required
| param | Data types |
|---|---|
| column | string |
| operator | string |
| value | string |
orWhere
Read more...
- Same as Where clausewhereRaw
- Allows you to use direct raw
SQL query syntax
whereColumn
- Takes three parameter
columnoperatorcolumn2
whereNull
- Takes one parameter
column
whereNotNull
Read more...
- Takes one parameter `column`whereBetween
- Takes two parameter
columnas stringparamas array- Doesn't support float value
| param | Data types | Value |
|---|---|---|
| column | string | column_name |
| param | array | [10, 100] |
whereNotBetween
Read more...
- Same as `whereBetween()` methodwhereIn
- Takes two parameter
columnas stringparamas array- Doesn't support float value
| param | Data types | Value |
|---|---|---|
| column | string | column_name |
| param | array | [0, 20, 80] |
whereNotIn
Read more...
Same as `whereIn()` methodgroupBy
- Takes one param
$column
Database Migration
- Similar to Laravel DB Migration
Just to make database table creation more easier
| method name | Returns |
|---|---|
| create() | Create table schema |
| run() | Begin migration |
| drop() | Drop migration tables |
Create Table Schema
- Takes param as string
$table- [optional] Second parameter
stringjobs|sessionsIf passed will create a dummyjobs|sessionstable schema - It's helper class can be called, using --
migration()
- [optional] Second parameter

Default String Length
- In some cases you may want to setup default string legnth to all Migration Tables
- It's helper class can be called, using --
schema()
- It's helper class can be called, using --
| 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 |
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
$tablenameas string$column_nameas string$valuesas mixed dataNULLNOT NULL\|NoneSTRINGcurrent_timestamp()
Run Migration
- This will execute and run migrations using files located at [root/database/migrations]
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 tablesDrop Table
Read more...
- Takes one param as `string` $table_nameDrop Column
Read more...
- To Drop Column `takes two param` - This will drop the column availableGet Database Config
Get Database Connection
- It's helper class can be called, using --
db_connection()
Get Database Name
Get Database PDO
Get Database TablePrefix
Database Import
- You can use this class to import
.sqlinto a database programatically- Take two param as
[$path|$connection] - Mandatory
$pathas string of path to .sql file - [optional]
$connectiondefine the connection of database you want to run
- Take two param as
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) |
Collation And Charset
- Collation and Charset Data
listing
Collation
- utf8_bin
- utf8_general_ci
- utf8mb4_bin
- utf8mb4_unicode_ci
- utf8mb4_general_ci
- latin1_bin
- latin1_general_ci
Charset
- utf8
- utf8mb4
- latin1
Extend Model Class
Read more...
- You can as well extends the DB Model class directly from other classHelpers 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 |
| migration() | Return instance of (new Migration) class |
| schema() | Return instance of (new Schema) class |
Error Dump
| function | Description |
|---|---|
| dump | Dump Data |
| dd | Dump and Die |
Error Status
- On error returns
404status code - On success returns
200status code
Useful Links
- @author Fredrick Peterson (Tame Developers)
- If you love this PHP Library, you can Buy Tame Developers a coffee
- Lightweight - PHP ORM Database
- Support - Library
