Skip to content

Commit 7b2622a

Browse files
take, groupByDesc, groupByAsc and readme update
1 parent 59456ac commit 7b2622a

File tree

5 files changed

+180
-28
lines changed

5 files changed

+180
-28
lines changed

README.md

Lines changed: 104 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,29 @@ was pretty tough. So i decided to create a much more easier way of communicating
2525
* [More Database Connection Keys](#more-database-connection-keys)
2626
* [Usage](#usage)
2727
* [Table](#table)
28-
* [Insert](#insert)
29-
* [Insert Or Ignore](#insert-or-ignore)
30-
* [Update](#update)
31-
* [Update Or Ignore](#update-or-ignore)
32-
* [Delete](#delete)
33-
* [Increment](#increment)
34-
* [Decrement](#decrement)
28+
* [insert](#insert)
29+
* [insertOrIgnore](#insert-or-ignore)
30+
* [update](#update)
31+
* [updateOrIgnore](#update-or-ignore)
32+
* [destroy](#destroy)
33+
* [delete](#delete)
34+
* [increment](#increment)
35+
* [decrement](#decrement)
36+
* [min](#min)
37+
* [max](#max)
38+
* [sum](#sum)
39+
* [avg](#avg)
40+
* [average](#average)
3541
* [Fetching Data](#fetching-data)
36-
* [Get](#get)
37-
* [First](#first)
38-
* [First or Create](#first-or-create)
39-
* [First or Fail](#first-or-fail)
40-
* [Count](#count)
41-
* [Paginate](#paginate)
42-
* [Exist](#exists)
43-
* [Table Exists](#table-exists)
42+
* [get](#get)
43+
* [first](#first)
44+
* [firstOrCreate](#first-or-create)
45+
* [firstOrFail](#first-or-fail)
46+
* [count](#count)
47+
* [paginate](#paginate)
48+
* [exists](#exists)
49+
* [doesntExist](#doesntExist)
50+
* [tableExists](#table-exists)
4451
* [Collections](#collections)
4552
* [Collection Methods](#collection-methods)
4653
* [Collection Usage](#collection-usage)
@@ -55,40 +62,79 @@ was pretty tough. So i decided to create a much more easier way of communicating
5562
* [Pagination Foreach Numbers](#pagination-foreach-numbers)
5663
* [Get Pagination](#get-pagination)
5764
* [Clause](#clause)
58-
* [Query](#query)
65+
* [query](#query)
5966
* [select](#select)
6067
* [selectRaw](#selectRaw)
6168
* [orderBy](#orderby)
6269
* [orderByRaw](#orderbyraw)
70+
* [orderByDesc](#orderByDesc)
71+
* [orderByAsc](#orderByAsc)
6372
* [latest](#latest)
6473
* [oldest](#oldest)
6574
* [inRandomOrder](#inRandomOrder)
6675
* [random](#random)
6776
* [limit](#limit)
77+
* [take](#take)
6878
* [offset](#offset)
6979
* [join](#join)
80+
* [joinWhere](#joinWhere)
7081
* [leftJoin](#leftJoin)
82+
* [leftJoinWhere](#leftJoinWhere)
83+
* [rightJoin](#rightJoin)
84+
* [rightJoinWhere](#rightJoinWhere)
85+
* [crossJoin](#crossJoin)
7186
* [where](#where)
7287
* [orWhere](#orwhere)
88+
* [whereNot](#whereNot)
89+
* [orWhereNot](#orWhereNot)
7390
* [whereRaw](#whereRaw)
7491
* [whereColumn](#wherecolumn)
92+
* [orWhereColumn](#orWhereColumn)
7593
* [whereNull](#wherenull)
7694
* [orWhereNull](#orWhereNull)
7795
* [whereNotNull](#wherenotnull)
7896
* [orWhereNotNull](#orWhereNotNull)
7997
* [whereBetween](#wherebetween)
98+
* [orWhereBetween](#orWhereBetween)
8099
* [whereNotBetween](#wherenotbetween)
100+
* [orWhereNotBetween](#orWhereNotBetween)
101+
* [whereBetweenColumns](#whereBetweenColumns)
102+
* [orWhereBetweenColumns](#orWhereBetweenColumns)
103+
* [whereNotBetweenColumns](#whereNotBetweenColumns)
104+
* [orWhereNotBetweenColumns](#orWhereNotBetweenColumns)
105+
* [whereDate](#whereDate)
106+
* [orWhereDate](#orWhereDate)
107+
* [whereTime](#whereTime)
108+
* [orWhereTime](#orWhereTime)
109+
* [whereDay](#whereDay)
110+
* [orWhereDay](#orWhereDay)
111+
* [whereMonth](#whereMonth)
112+
* [orWhereMonth](#orWhereMonth)
113+
* [whereYear](#whereYear)
114+
* [orWhereYear](#orWhereYear)
115+
* [having](#having)
116+
* [orHaving](#orHaving)
117+
* [havingNull](#havingNull)
118+
* [orHavingNull](#orHavingNull)
119+
* [havingNotNull](#havingNotNull)
120+
* [orHavingNotNull](#orHavingNotNull)
121+
* [havingBetween](#havingBetween)
122+
* [havingRaw](#havingRaw)
81123
* [whereIn](#wherein)
124+
* [orWhereIn](#orWhereIn)
82125
* [whereNotIn](#wherenotin)
126+
* [orWhereNotIn](#orWhereNotIn)
127+
* [orHavingRaw](#orHavingRaw)
83128
* [groupBy](#groupby)
129+
* [groupByRaw](#groupByRaw)
84130
* [Database Migration](#database-migration)
85131
* [Create Table Schema](#create-table-schema)
86132
* [Default String Length](#default-string-length)
87133
* [Update Column Default Value](#update-column-default-value)
88134
* [Run Migration](#run-migration)
89135
* [Drop Migration](#drop-migration)
90-
* [Drop Table](#drop-table)
91-
* [Drop Column](#drop-column)
136+
* [dropTable](#drop-table)
137+
* [dropColumn](#drop-column)
92138
* [Get Database Config](#get-database-config)
93139
* [Get Database Connection](#get-database-connection)
94140
* [Get Database Name](#get-database-name)
@@ -283,7 +329,7 @@ $db->table('users')
283329
]);
284330
```
285331

286-
### Delete
332+
### delete
287333
- Returns an `int`
288334

289335
```
@@ -292,6 +338,20 @@ $db->table('users')
292338
->delete();
293339
```
294340

341+
### destroy
342+
- Take two param as `[value|column]`
343+
- Mandatory `value` as mixed value
344+
- [optional] `column` as Default is `id`
345+
- Returns an `int`
346+
347+
```
348+
$db->table('posts')->destroy(1);
349+
// Query: delete from `posts` where `id` = ?
350+
351+
$db->table('posts')->destroy(10, 'post_id');
352+
// Query: delete from `posts` where `post_id` = ?
353+
```
354+
295355
### Increment
296356

297357
- Takes three parameter
@@ -347,6 +407,31 @@ $db->table('users')
347407
]);
348408
```
349409

410+
### min
411+
- Take one param as `Expression|string`
412+
```
413+
$db->table('blog')->min('amount');
414+
```
415+
416+
### max
417+
- Same as min
418+
```
419+
$db->table('blog')->max('amount');
420+
```
421+
422+
### sum
423+
- Take one param as `Expression|string`
424+
```
425+
$db->table('blog')->sum('amount');
426+
```
427+
428+
### avg
429+
- Take one param as `Expression|string`
430+
```
431+
$db->table('blog')->avg('amount');
432+
$db->table('blog')->average('amount');
433+
```
434+
350435
## Fetching Data
351436

352437
| object name | Returns |

src/Schema/Builder.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function query(string $query)
6363
* Execute the query strings
6464
*
6565
* @return int
66+
*
67+
* @throws \Exception
6668
*/
6769
public function exec()
6870
{
@@ -112,6 +114,8 @@ public function selectRaw(mixed $expression, $bindings = [])
112114
* [optional] Default direction is `asc`
113115
*
114116
* @return $this
117+
*
118+
* @throws \Exception
115119
*/
116120
public function orderBy($column, $direction = 'asc')
117121
{
@@ -126,6 +130,30 @@ public function orderBy($column, $direction = 'asc')
126130
return $this;
127131
}
128132

133+
/**
134+
* Add an "order by desc" clause to the query.
135+
*
136+
* @param string $column
137+
*
138+
* @return $this
139+
*/
140+
public function orderByDesc($column)
141+
{
142+
return $this->orderBy($column, 'desc');
143+
}
144+
145+
/**
146+
* Add an "order by asc" clause to the query.
147+
*
148+
* @param string $column
149+
*
150+
* @return $this
151+
*/
152+
public function orderByAsc($column)
153+
{
154+
return $this->orderBy($column, 'asc');
155+
}
156+
129157
/**
130158
* Add a raw "order by" clause to the query.
131159
*
@@ -152,7 +180,7 @@ public function orderByRaw(string $sql)
152180
*/
153181
public function latest(string $column = 'id')
154182
{
155-
$this->orderBy($column, 'desc');
183+
$this->orderByDesc($column);
156184

157185
return $this;
158186
}
@@ -167,7 +195,7 @@ public function latest(string $column = 'id')
167195
*/
168196
public function oldest(string $column = 'id')
169197
{
170-
$this->orderBy($column);
198+
$this->orderByAsc($column);
171199

172200
return $this;
173201
}
@@ -204,7 +232,7 @@ public function random()
204232
}
205233

206234
/**
207-
* Set limits
235+
* Set limit for query
208236
*
209237
* @param int $value
210238
* @return $this
@@ -218,6 +246,17 @@ public function limit($value)
218246
return $this;
219247
}
220248

249+
/**
250+
* Alias for the "limit" method.
251+
*
252+
* @param int $value
253+
* @return $this
254+
*/
255+
public function take($value)
256+
{
257+
return $this->limit($value);
258+
}
259+
221260
/**
222261
* Set the "offset" value of the query.
223262
*
@@ -666,6 +705,8 @@ public function orWhereRaw($sql, $bindings = [])
666705
* @param string $boolean
667706
* @param bool $not
668707
* @return $this
708+
*
709+
* @throws \Exception
669710
*/
670711
public function whereIn($column, $values, $boolean = 'and', $not = false)
671712
{
@@ -1553,7 +1594,7 @@ public function delete()
15531594

15541595
$sql = $this->compile()->compileDelete($this);
15551596

1556-
// replace all quoteation to backticks
1597+
// replace all quotation to backticks
15571598
$sql = str_replace(['`', "'"], "`", $sql);
15581599

15591600
// delete query
@@ -1568,16 +1609,16 @@ public function delete()
15681609
* Destroy records from the database.
15691610
* [performing where clause under the hood]
15701611
*
1571-
* @param mixed $id
1612+
* @param mixed $value
15721613
*
15731614
* @param string $column
15741615
* [default column name is 'id']
15751616
*
15761617
* @return int
15771618
*/
1578-
public function destroy($id, $column = 'id')
1619+
public function destroy($value, $column = 'id')
15791620
{
1580-
return $this->where($column, $id)->delete();
1621+
return $this->where($column, $value)->delete();
15811622
}
15821623

15831624
/**

src/Schema/Traits/BuilderTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
trait BuilderTrait{
2929

3030
/**
31-
* Builder Compiler Instance
31+
* Get Instance Table Name
3232
*
3333
* @return string
3434
*/
@@ -37,6 +37,16 @@ public function tableName()
3737
return "{$this->connection->tablePrefix}{$this->from}";
3838
}
3939

40+
/**
41+
* Get Instance Table Name
42+
*
43+
* @return string
44+
*/
45+
public function getTableName()
46+
{
47+
return $this->tableName();
48+
}
49+
4050
/**
4151
* Set callback method
4252
* @return void

tests/Model/Admin.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ class Admin extends Model{
99

1010
public function getUsers()
1111
{
12+
dd(
13+
$this->getTableName(),
14+
$this->average('user_id')
15+
);
16+
1217
return $this->where('active', 1)
1318
->join('tb_orders_payment', 'tb_orders_payment.user_id', '=', 'tb_user.user_id')
1419
->join('tb_wallet', 'tb_wallet.user_id', '=', 'tb_user.user_id')
15-
->limit(10)
20+
// ->limit(10)
21+
->take(10)
1622
->get();
1723
}
1824

tests/testModel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44

55
include_once __DIR__ . "/../vendor/autoload.php";
66
include_once __DIR__ . "/Model/Post.php";
7+
include_once __DIR__ . "/Model/Admin.php";
78

89
// as long as we're not including the init.php file
910
// then we must boot into out app to start using package
1011
AppManager::bootLoader();
1112

13+
14+
$db = db();
15+
$admin = new Admin;
16+
17+
dd(
18+
$db,
19+
$admin->getUsers()
20+
);
21+
1222
// using model
1323
Post::where('id', 2)->get();
1424

0 commit comments

Comments
 (0)