Skip to content

Commit 4b05a53

Browse files
authored
Merge pull request #2 from classid/patch/value-on-builder-from-string-to-mixed
Patch/value on builder from string to mixed
2 parents 6468d67 + 128d5d8 commit 4b05a53

File tree

4 files changed

+65
-47
lines changed

4 files changed

+65
-47
lines changed

src/Config/queryextend.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
| per page is 10
1111
|
1212
*/
13-
"perpage" => 15,
13+
"perpage" =>[
14+
"key" => "perpage",
15+
"value" => 15,
16+
],
1417

1518
/*
1619
|--------------------------------------------------------------------------

src/Contracts/Abstracts/BaseQueryBuilder.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @method getAllData(array $whereClause = [], array $columns = ["*"])
1919
* @method static getDataById(string|int|array $id, array $columns = ["*"])
2020
* @method getDataById(string|int|array $id, array $columns = ["*"])
21-
* @method static getSingleData(array $whereClause = [], array $columns = ["*"])
21+
* @method static getSingleData(array $whereClause = [], array $columns = ["*"])
2222
* @method getSingleData(array $whereClause = [], array $columns = ["*"])
2323
* @method static addNewData(array $requestedData)
2424
* @method addNewData(array $requestedData)
@@ -63,16 +63,16 @@
6363
* @method BaseQueryBuilder whereNotBetweenColumns(string $column, array $values, string $boolean = 'and')
6464
* @method static BaseQueryBuilder whereBetweenColumns(string $column, array $values, string $boolean = 'and', bool $not = false)
6565
* @method BaseQueryBuilder whereBetweenColumns(string $column, array $values, string $boolean = 'and', bool $not = false)
66-
* @method static BaseQueryBuilder whereNotBetween(string $column, array|string $values, string $boolean = 'and')
67-
* @method BaseQueryBuilder whereNotBetween(string $column, array|string $values, string $boolean = 'and')
68-
* @method static BaseQueryBuilder whereBetween(string $column, array $values, string $boolean = 'and', bool $not = false)
69-
* @method BaseQueryBuilder whereBetween(string $column, array $values, string $boolean = 'and', bool $not = false)
70-
* @method static BaseQueryBuilder whereNot($column, ?string $operator = null, ?string $value = null, ?string $boolean = 'and')
71-
* @method BaseQueryBuilder whereNot($column, ?string $operator = null, ?string $value = null, ?string $boolean = 'and')
72-
* @method static BaseQueryBuilder orWhere(array|string $column, ?string $operator = null, ?string $value = null)
73-
* @method BaseQueryBuilder orWhere(array|string $column, ?string $operator = null, ?string $value = null)
74-
* @method static BaseQueryBuilder where(array|string $column, ?string $operator = null, ?string $value = null, ?string $boolean = 'and')
75-
* @method BaseQueryBuilder where(array|string $column, ?string $operator = null, ?string $value = null, ?string $boolean = 'and')
66+
* @method static BaseQueryBuilder whereNotBetween(string $column, iterable $values, string $boolean = 'and')
67+
* @method BaseQueryBuilder whereNotBetween(string $column, iterable $values, string $boolean = 'and')
68+
* @method static BaseQueryBuilder whereBetween(string $column, iterable $values, string $boolean = 'and', bool $not = false)
69+
* @method BaseQueryBuilder whereBetween(string $column, iterable $values, string $boolean = 'and', bool $not = false)
70+
* @method static BaseQueryBuilder whereNot($column, ?string $operator = null, mixed $value = null, ?string $boolean = 'and')
71+
* @method BaseQueryBuilder whereNot($column, ?string $operator = null, mixed $value = null, ?string $boolean = 'and')
72+
* @method static BaseQueryBuilder orWhere(array|string $column, ?string $operator = null, mixed $value = null)
73+
* @method BaseQueryBuilder orWhere(array|string $column, ?string $operator = null, mixed $value = null)
74+
* @method static BaseQueryBuilder where(array|string $column, ?string $operator = null, mixed $value = null, ?string $boolean = 'and')
75+
* @method BaseQueryBuilder where(array|string $column, ?string $operator = null, mixed $value = null, ?string $boolean = 'and')
7676
* @method static BaseQueryBuilder orWhereHas(string $relation, Closure|null $callback = null, string $operator = '>=', int $count = 1)
7777
* @method BaseQueryBuilder orWhereHas(string $relation, Closure|null $callback = null, string $operator = '>=', int $count = 1)
7878
* @method static BaseQueryBuilder whereHas(string $relation, Closure|null $callback = null, string $operator = '>=', int $count = 1)
@@ -140,7 +140,7 @@ public function build(): Builder
140140
*/
141141
public static function init(): self
142142
{
143-
$class = get_called_class();
143+
$class = static::class;
144144
return new $class;
145145
}
146146

src/Contracts/Abstracts/BaseQueryBuilderExtend.php

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Illuminate\Database\Eloquent\Relations\Relation;
1414
use Illuminate\Validation\ValidationException;
1515

16+
/**
17+
* @template T
18+
*/
1619
class BaseQueryBuilderExtend
1720
{
1821
use QueryOrder {
@@ -217,11 +220,11 @@ public function orWhereHas(string $relation, Closure|null $callback = null, stri
217220
/**
218221
* @param array|string $column
219222
* @param string|null $operator
220-
* @param string|null $value
223+
* @param mixed $value
221224
* @param string|null $boolean
222225
* @return BaseQueryBuilder
223226
*/
224-
public function where(array|string $column, ?string $operator = null, ?string $value = null, ?string $boolean = 'and'): BaseQueryBuilder
227+
public function where(array|string $column, ?string $operator = null, mixed $value = null, ?string $boolean = 'and'): BaseQueryBuilder
225228
{
226229
$this->builder->where($column, $operator, $value, $boolean);
227230
return $this->baseQueryBuilder;
@@ -231,10 +234,10 @@ public function where(array|string $column, ?string $operator = null, ?string $v
231234
/**
232235
* @param array|string $column
233236
* @param string|null $operator
234-
* @param string|null $value
237+
* @param mixed $value
235238
* @return BaseQueryBuilder
236239
*/
237-
public function orWhere(array|string $column, ?string $operator = null, ?string $value = null): BaseQueryBuilder
240+
public function orWhere(array|string $column, ?string $operator = null, mixed $value = null): BaseQueryBuilder
238241
{
239242
$this->builder->orWhere($column, $operator, $value);
240243
return $this->baseQueryBuilder;
@@ -244,11 +247,11 @@ public function orWhere(array|string $column, ?string $operator = null, ?string
244247
/**
245248
* @param $column
246249
* @param string|null $operator
247-
* @param string|null $value
250+
* @param mixed $value
248251
* @param string|null $boolean
249252
* @return BaseQueryBuilder
250253
*/
251-
public function whereNot($column, ?string $operator = null, ?string $value = null, ?string $boolean = 'and'): BaseQueryBuilder
254+
public function whereNot($column, ?string $operator = null, mixed $value = null, ?string $boolean = 'and'): BaseQueryBuilder
252255
{
253256
$this->builder->whereNot($column, $operator, $value, $boolean);
254257
return $this->baseQueryBuilder;
@@ -257,12 +260,12 @@ public function whereNot($column, ?string $operator = null, ?string $value = nul
257260

258261
/**
259262
* @param string $column
260-
* @param array $values
263+
* @param iterable $values
261264
* @param string $boolean
262265
* @param bool $not
263266
* @return BaseQueryBuilder
264267
*/
265-
public function whereBetween(string $column, array $values, string $boolean = 'and', bool $not = false): BaseQueryBuilder
268+
public function whereBetween(string $column, iterable $values, string $boolean = 'and', bool $not = false): BaseQueryBuilder
266269
{
267270
$this->builder->whereBetween($column, $values, $boolean, $not);
268271
return $this->baseQueryBuilder;
@@ -271,11 +274,11 @@ public function whereBetween(string $column, array $values, string $boolean = 'a
271274

272275
/**
273276
* @param string $column
274-
* @param array|string $values
277+
* @param iterable $values
275278
* @param string $boolean
276279
* @return BaseQueryBuilder
277280
*/
278-
public function whereNotBetween(string $column, array|string $values, string $boolean = 'and'): BaseQueryBuilder
281+
public function whereNotBetween(string $column, iterable $values, string $boolean = 'and'): BaseQueryBuilder
279282
{
280283
$this->builder->whereNotBetween($column, $values, $boolean);
281284
return $this->baseQueryBuilder;
@@ -453,62 +456,73 @@ public function orWhereColumn(array|string $first, ?string $operator = null, ?st
453456

454457
/**
455458
* @param array $whereClause
456-
* @param array $columns
459+
* @param array|null $columns
457460
* @param int|null $perPage
458461
* @return LengthAwarePaginator
459462
*/
460-
public function getAllDataPaginated(array $whereClause = [], array $columns = ["*"], ?int $perPage = null):LengthAwarePaginator
463+
public function getAllDataPaginated(array $whereClause = [], array|null $columns = null, ?int $perPage = null): LengthAwarePaginator
461464
{
462465
if (!$perPage) {
463-
$perPage = request()->query("perpage", config('queryextend.perpage'));
466+
$perPage = request()->query(config("queryextend.perpage.key"), config('queryextend.perpage.value'));
467+
}
468+
469+
if ($columns) {
470+
$this->builder->addSelect($columns);
464471
}
465472
return $this->builder
466-
->select($columns)
467473
->where($whereClause)
468474
->paginate($perPage);
469475
}
470476

471477
/**
472478
* @param array $whereClause
473-
* @param array $columns
474-
* @return Builder[]|Collection
479+
* @param array|null $columns
480+
* @return Collection
475481
*/
476-
public function getAllData(array $whereClause = [], array $columns = ["*"]): Collection|array
482+
public function getAllData(array $whereClause = [], array|null $columns = null): Collection
477483
{
484+
if ($columns) {
485+
$this->builder->addSelect($columns);
486+
}
478487
return $this->builder
479-
->select($columns)
480488
->where($whereClause)
481489
->get();
482490
}
483491

484492
/**
485493
* @param string|int|array $id
486-
* @param array $columns
487-
* @return Builder|Builder[]|Collection|Model|null
494+
* @param array|null $columns
495+
* @return Builder|Builder[]|Collection|T|null
488496
*/
489-
public function getDataById(string|int|array $id, array $columns = ["*"])
497+
public function getDataById(string|int|array $id, array|null $columns = null): Model|Collection|Builder|array|null
490498
{
491-
return $this->builder->select($columns)->find($id);
499+
if ($columns) {
500+
$this->builder->addSelect($columns);
501+
}
502+
return $this->builder->find($id);
492503
}
493504

494505
/**
495506
* @param array $whereClause
496-
* @param array $columns
497-
* @return Builder|Model|object|null
507+
* @param array|null $columns
508+
* @return Builder|T|null
498509
*/
499-
public function getSingleData(array $whereClause = [], array $columns = ["*"])
510+
public function getSingleData(array $whereClause = [], array|null $columns = null): Model|Builder|null
500511
{
512+
if ($columns) {
513+
$this->builder->addSelect($columns);
514+
}
501515
return $this->builder
502-
->select($columns)
503516
->where($whereClause)
504517
->first();
505518
}
506519

507520
/**
521+
*
508522
* @param array $requestedData
509-
* @return Builder|Model
523+
* @return T|Builder
510524
*/
511-
public function addNewData(array $requestedData)
525+
public function addNewData(array $requestedData): Model|Builder
512526
{
513527
return $this->builder->create($requestedData);
514528
}
@@ -531,7 +545,7 @@ public function addMultipleData(array $requestedData): int
531545
* @param bool $isReturnObject
532546
* @return Builder|Builder[]|Collection|Model|int|null
533547
*/
534-
public function updateDataById(string|int $id, array $requestedData, array $columns = ["*"], bool $isReturnObject = true)
548+
public function updateDataById(string|int $id, array $requestedData, array $columns = ["*"], bool $isReturnObject = true): Model|Collection|Builder|int|array|null
535549
{
536550
$updatedData = $this->builder
537551
->where("id", $id)
@@ -547,9 +561,9 @@ public function updateDataById(string|int $id, array $requestedData, array $colu
547561
* @param array $requestedData
548562
* @param array $columns
549563
* @param bool $isReturnObject
550-
* @return Collection|int|null
564+
* @return Collection|int
551565
*/
552-
public function updateDataByWhereClause(array $whereClause, array $requestedData, array $columns = ["*"], bool $isReturnObject = false)
566+
public function updateDataByWhereClause(array $whereClause, array $requestedData, array $columns = ["*"], bool $isReturnObject = false): Collection|int
553567
{
554568
$updatedData = $this->builder
555569
->where($whereClause)
@@ -563,7 +577,7 @@ public function updateDataByWhereClause(array $whereClause, array $requestedData
563577
* @param string|int $id
564578
* @return mixed
565579
*/
566-
public function deleteDataById(string|int $id)
580+
public function deleteDataById(string|int $id): mixed
567581
{
568582
return $this->builder
569583
->where("id", $id)
@@ -574,7 +588,7 @@ public function deleteDataById(string|int $id)
574588
* @param array $whereClause
575589
* @return mixed
576590
*/
577-
public function deleteDataByWhereClause(array $whereClause)
591+
public function deleteDataByWhereClause(array $whereClause): mixed
578592
{
579593
return $this->builder
580594
->where($whereClause)

src/Traits/QueryExtend.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Classid\LaravelServiceQueryBuilderExtend\Contracts\Abstracts\BaseQueryBuilder;
66
use Classid\LaravelServiceQueryBuilderExtend\Contracts\Abstracts\BaseQueryBuilderExtend;
77
use Exception;
8+
use RuntimeException;
89

910
trait QueryExtend
1011
{
@@ -42,7 +43,7 @@ public function __call($name, $arguments)
4243
public function overload(string $name, array $arguments): mixed
4344
{
4445
if (!property_exists($this, 'builder')) {
45-
throw new Exception("Property 'builder' does not exist or is not initialized.");
46+
throw new RuntimeException("Property 'builder' does not exist or is not initialized.");
4647
}
4748

4849
if (method_exists(new BaseQueryBuilderExtend($this), $name)) {

0 commit comments

Comments
 (0)