wpQueryBuilder provides a convenient, fluent interface to run database queries. It can be used to perform most database operations in your application.
via Composer: composer require alemran/wp-query-builder
require 'vendor/autoload.php';
use wpQueryBuilder\DB;
DB::table('demo')->first()
DB Functions
- Retrieving A Single Row
DB::table('users')->first();
- Retrieving A multiple Row
DB::table('users')->get();
where($column, $value )
:DB::table('users') ->where('id', 1) ->get()
DB::table('users') ->where(function($query){ $query->where('id', 1); $query->orWhere('name', "name"); }) ->get()
orWhere($column, $value)
:DB::table('users') ->where('id', 1) ->orWhere('name', "name") ->get()
DB::table('users') ->where('id', 1) ->orWhere(function($query){ $query->where('field', 'value); $query->where('field', 'value); }) ->first()
whereRaw($query)
:DB::table('users') ->whereRaw('id = 1') ->first()
orWhereRaw($query)
:DB::table('users') ->whereRaw('id = 1') ->orWhereRaw('id = 1') ->first()
orderBy($columns, $direction)
:DB::table('users') ->orderBy('id', 'desc')
DB::table('users') ->orderBy('id,name', 'desc')
groupBy($columns)
:DB::table('users') ->groupBy('id')
DB::table('users') ->groupBy('id,name')
limit($number)
:DB::table('users') ->where('id', 1) ->limit(number)->get()
offset($number)
:DB::table('users') ->where('id', 1) ->limit(number)->offset(number)->get()
select($fields)
:DB::table('users') ->select('id,name') ->get()
insert($data)
:DB::table('users') ->insert(['name' => "demo"])
update($data,$where)
:DB::table('users') ->where('id', 1) ->update(['name' => "demo"])
delete($where)
:DB::table('users') ->where('id', 1) ->delete()
join($table, $first, $operator = null, $second = null) (INNER JOIN)
:DB::table('demo_notes as n') ->join('demo_users as u', 'u.id', '=', 'n.user_id') ->first()
DB::table('demo_notes as n') ->join('demo_users as u', function($query){ $query->on( 'u.id', '=', 'n.user_id') $query->orOn( 'u.id', '=', 'n.user_id') }) ->first()
DB::table('demo_notes as n') ->join('demo_users as u', function($query) use($request){ $query->on( 'u.id', '=', 'n.user_id') $query->onWhere( 'u.id', '=', $request->id) }) ->first()
Note: Must use table alias for using join or leftJoin.
-
leftJoin($table, $first, $operator = null, $second = null) (LEFT JOIN)
: Same as join() transaction()
:DB::startTransaction(function(){ DB::table('demo_notes') ->insert([ "note" => "Hello", ]); })
DB::startTransaction(function(DB $query){ $query->table('demo_notes') ->insert([ "note" => "Hello", ]); })