Skip to content

Commit

Permalink
Merge pull request #3 from crocodic-studio/master
Browse files Browse the repository at this point in the history
Add exception in withTable
  • Loading branch information
fherryfherry authored Mar 13, 2021
2 parents c71c546 + 487182a commit 3513228
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Core/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
/**
* Class Builder
* @package Crocodic\LaravelModel\Core
* @method Builder addSelectTable(string $table)
* @method Builder withTable($table)
* @method Builder like($column, $keyword)
* @method Builder addSelectTable(string $table, string $prefix = null, array $exceptColumns = [])
* @method Builder withTable(string $table, string $selectPrefix = null, array $exceptColumns = [], string $first = null, string $operator = null, string $second = null)
* @method Builder like(string $column, string $keyword)
*/
abstract class Builder extends \Illuminate\Database\Query\Builder
{
Expand Down
22 changes: 14 additions & 8 deletions src/Helpers/BuilderMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@ class BuilderMacro
{
public static function registerMacro()
{
Builder::macro("addSelectTable", function($table) {
Builder::macro("addSelectTable", function($table, $prefix = null, $exceptColumns = []) {
$fields = Helper::getFields($table);
foreach($fields as $field) {
$this->addSelect($table.".".$field." as ".$table."_".$field);
if(count($exceptColumns) && in_array($field,$exceptColumns)) continue;
$prefix = ($prefix) ? $prefix : $table;
$this->addSelect($table.".".$field." as ".$prefix."_".$field);
}
return $this;
});

Builder::macro("withTable", function($table) {
Builder::macro("withTable", function($table, $selectionPrefix = null, $exceptColumns = [], $first = null, $operator = "=", $second = null) {
/** @var \Crocodic\LaravelModel\Core\Builder $this */
if(is_array($table)) {
foreach($table as $tbl) {
$this->leftJoin($tbl,$tbl.".id","=",$this->from."_id");
$this->addSelectTable($tbl);
$first = ($first) ? $first : $tbl.".id";
$second = ($second) ? $second : $this->from."_id";
$this->leftJoin($tbl,$first,$operator,$second);
$this->addSelectTable($tbl, $selectionPrefix, $exceptColumns);
}
} else {
$this->leftJoin($table, $table.".id", "=", $table."_id");
$this->addSelectTable($table);
$first = ($first) ? $first : $table.".id";
$second = ($second) ? $second : $table."_id";
$this->leftJoin($table, $first, $operator, $second);
$this->addSelectTable($table, $selectionPrefix, $exceptColumns);
}
return $this;
});
Expand All @@ -35,7 +41,7 @@ public static function registerMacro()
if(substr($keyword,0,1) != "%" && substr($keyword,-1,1) != "%") {
$keyword = "%".$keyword."%";
}
$this->whereRaw($column." like '".$keyword."'");
$this->where($column, "like", "%".$keyword."%");
});
}
}

0 comments on commit 3513228

Please sign in to comment.