Skip to content

Commit

Permalink
Merge pull request #1 from crocodic-studio/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
fherryfherry authored Mar 13, 2021
2 parents a5925da + 1994993 commit 4caf00e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ $result = FooBar::table()
->addSelectTable("related_table") // This will produce: related_table_id, related_table_created_at, etc
->first();

/**
* Add like condition to the query
*/
$result = FooBar::table()->like("column",$yourKeyword)->get();
// It will produce same as FooBar::table()->where("columne","like","%".$yourKeyword."%")->get()

/**
* Find a record by a specific condition
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
/**
* Class Builder
* @package Crocodic\LaravelModel\Core
* @method \Illuminate\Database\Query\Builder addSelectTable(string $table)
* @method \Illuminate\Database\Query\Builder withTable($table)
* @method Builder addSelectTable(string $table)
* @method Builder withTable($table)
* @method Builder like($column, $keyword)
*/
abstract class Builder extends \Illuminate\Database\Query\Builder
{
Expand Down
10 changes: 9 additions & 1 deletion src/Helpers/BuilderMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function registerMacro()
});

Builder::macro("withTable", function($table) {
/** @var Builder $this */
/** @var \Crocodic\LaravelModel\Core\Builder $this */
if(is_array($table)) {
foreach($table as $tbl) {
$this->leftJoin($tbl,$tbl.".id","=",$this->from."_id");
Expand All @@ -29,5 +29,13 @@ public static function registerMacro()
}
return $this;
});

Builder::macro("like", function($column, $keyword) {
/** @var \Crocodic\LaravelModel\Core\Builder $this */
if(substr($keyword,0,1) != "%" && substr($keyword,-1,1) != "%") {
$keyword = "%".$keyword."%";
}
$this->whereRaw($column." like '".$keyword."'");
});
}
}

0 comments on commit 4caf00e

Please sign in to comment.