Skip to content

Latest commit

 

History

History
203 lines (151 loc) · 2.93 KB

README.md

File metadata and controls

203 lines (151 loc) · 2.93 KB

PHP PDO Basic Query Builder

It is a similar version of the laravel database engine.

Configuration

$params = [
    'host' => 'localhost',
    'dbname' => 'migration_exam',
    'user' => 'root',
    'password' => 'root'
];

$builder = new \DBQuery\Builder($params)

Insert

$builder->table('users')
    ->insert([
   'name'=>'aa',
   'surname'=>'bb',
   'username' => 'aabb' 
]);

Update

$builder->table('users')
    ->where('id',9)
    ->update([
        'name'=>'cc' 
]);

Delete

$builder->table('users')
    ->where('id',9)
    ->delete();

Delete with id

$builder->table('users')
    ->delete(9);

Increment and Decrement

$builder->table('users')
    ->increment('number');

//With id
$builder->table('users')
    ->increment('number',['id'=>9]);

$builder->table('users')
    ->decrement('number');

//With id
$builder->table('users')
    ->decrement('number',['id'=>9]);

Count, Avg, Sum, Min, Max

$builder->table('users')
    ->count();

$builder->table('users')
    ->avg();

$builder->table('users')
    ->sum('number');

$builder->table('users')
    ->min();

$builder->table('users')
    ->max();

where, whereNull, whereIn

$builder->table('users')
    ->where('id',9)
    ->get();

$builder->table('users')
    ->whereNull('name')
    ->get();

$builder->table('users')
    ->whereIn('name',[1,2,3,4])
    ->get();

Returns the data of the specified table

$builder->table('users')
    ->get();

//or by column name
$builder->table('users')
    ->get('name');

updateOrInsert()

$builder->table('users')
//The values in the first array exist in the database, and update it with the second array, but if not, it creates a new record in the database. 
    ->updateOrInsert(
        ['name'=>'aa','surname'=>'bb'],
        ['username'=>'aa_new']
    );

insertGetId()

$builder->table('users')
//Returns the last id of the inserted data. 
    ->insertGetId([
        'name'=>'aa','surname'=>'bb'
    ]);

truncate()

$builder->table('users')
    ->truncate();

first()

$builder->table('users')
    ->first();
    
$builder->table('users')
    ->first('name');

$builder->table('users')
    ->where('id',9)
    ->first();

find()

$builder->table('users')
    ->find(9);

exists()

$builder->table('users')
    ->where('id',9)
    ->exists();

Joins

$builder->table('users')
    ->join('profile_images','users.id','=','profile_images.user_id')
    ->select('users.*','profile_images.link')
    ->get();

OrderBy

$builder->table('users')
    ->orderBy('created_at','DESC')
    ->get();

$builder->table('users')
    ->orderBy('created_at','ASC')
    ->get();

$builder->table('users')
    ->orderByDesc('created_at')
    ->get();

$builder->table('users')
    ->orderByAsc('created_at')
    ->get();