From 750c2ba62c621a478683e7daf775c19f40e47c3b Mon Sep 17 00:00:00 2001 From: Vincent Garnier Date: Tue, 11 Sep 2018 17:29:07 +0200 Subject: [PATCH 1/7] add Laravel 5.7 support --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 441db4b..d7fb2c6 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "license": "MIT", "require": { - "laravel/framework": "5.4.*|5.5.*|5.6.*" + "laravel/framework": "5.4.*|5.5.*|5.6.*|5.7.*" }, "autoload": { "psr-4": { From 9d47a44233bf1c1f4d80224916b362af2eb92a0d Mon Sep 17 00:00:00 2001 From: Vincent Garnier Date: Tue, 11 Sep 2018 17:30:06 +0200 Subject: [PATCH 2/7] add support for Laravel 5.7 --- changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/changelog.md b/changelog.md index 0dbfb16..5b03b0e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,11 @@ Changelog for Laravel Database Extension ======================================== +1.1.0 (2018-09-11) +------------------ + +- add support for Laravel 5.7 + 1.0.0 (2018-07-20) ------------------ From d992d3115da6a385b1d5fdec1146c7a7e2174322 Mon Sep 17 00:00:00 2001 From: Vincent Garnier Date: Thu, 7 Mar 2019 21:16:58 +0100 Subject: [PATCH 3/7] Add support for Laravel 5.8 --- changelog.md | 9 +++++++-- composer.json | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 5b03b0e..8e308fe 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ -Changelog for Laravel Database Extension -======================================== +Changelog +========= + +1.2.0 (2019-03-07) +------------------ + +- Add support for Laravel 5.8 1.1.0 (2018-09-11) ------------------ diff --git a/composer.json b/composer.json index d7fb2c6..758be61 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "license": "MIT", "require": { - "laravel/framework": "5.4.*|5.5.*|5.6.*|5.7.*" + "laravel/framework": "5.4.*|5.5.*|5.6.*|5.7.*|5.8.*" }, "autoload": { "psr-4": { From 998ec19007f54b6f7ff49239c1b1bc9a4cf1ccf1 Mon Sep 17 00:00:00 2001 From: Vincent Garnier Date: Mon, 11 Mar 2019 09:08:54 +0100 Subject: [PATCH 4/7] Replace getForeignKey method call by getForeignKeyName --- src/Eloquent/JoinRelBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Eloquent/JoinRelBuilder.php b/src/Eloquent/JoinRelBuilder.php index 95c8231..c24b436 100644 --- a/src/Eloquent/JoinRelBuilder.php +++ b/src/Eloquent/JoinRelBuilder.php @@ -183,7 +183,7 @@ protected function getRelationKeys(Relation $relation) elseif ($relation instanceof BelongsTo) { return [ $relation->getRelated()->getQualifiedKeyName(), - $relation->getParent()->getTable().'.'.$relation->getForeignKey() + $relation->getParent()->getTable().'.'.$relation->getForeignKeyName() ]; } From c4fb4c4e15a86d493bf2ce125d28eda92fbfe801 Mon Sep 17 00:00:00 2001 From: Vincent Garnier Date: Mon, 11 Mar 2019 09:09:25 +0100 Subject: [PATCH 5/7] Drop support for Laravel < 5.8 --- changelog.md | 6 ++++++ composer.json | 2 +- readme.md | 16 +--------------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/changelog.md b/changelog.md index 8e308fe..9279220 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,12 @@ Changelog ========= +2.0.0 (2019-03-11) +------------------ + +- Drop support for Laravel < 5.8 +- Replace getForeignKey method call by getForeignKeyName + 1.2.0 (2019-03-07) ------------------ diff --git a/composer.json b/composer.json index 758be61..395ff5f 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "license": "MIT", "require": { - "laravel/framework": "5.4.*|5.5.*|5.6.*|5.7.*|5.8.*" + "laravel/framework": "5.8.*" }, "autoload": { "psr-4": { diff --git a/readme.md b/readme.md index 47ad684..48397d2 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ Laravel Database Extension ========================== -Includes some extensions/improvements to the Database section of Laravel Framework 5.4+ +Includes some extensions/improvements to the Database section of Laravel Framework * [Installation](#installation) * [Usage](#usage) @@ -18,20 +18,6 @@ With Composer: composer require axn/laravel-database-extension ``` -In Laravel 5.5 the service provider is automatically included. -In older versions of the framework, simply add this service provider to the array -of providers in `config/app.php`: - -```php -// config/app.php - -'provider' => [ - //... - Axn\Illuminate\ServiceProvider::class, - //... -]; -``` - Usage ----- From 476c1b93c3b1f2d51a0ed3fa37c50a0ef9322b60 Mon Sep 17 00:00:00 2001 From: Vincent Garnier Date: Wed, 20 Mar 2019 14:09:51 +0100 Subject: [PATCH 6/7] Add an Eloquent "whereLike" query builder macro --- changelog.md | 9 +++++++-- readme.md | 41 +++++++++++++++++++++++++++++++++++++++++ src/ServiceProvider.php | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 9279220..acb4caf 100644 --- a/changelog.md +++ b/changelog.md @@ -1,11 +1,16 @@ Changelog ========= +2.1.0 (2019-03-20) +------------------ + +- Add an Eloquent "whereLike" query builder macro + 2.0.0 (2019-03-11) ------------------ -- Drop support for Laravel < 5.8 -- Replace getForeignKey method call by getForeignKeyName +- Drop support for Laravel < 5.8 +- Replace getForeignKey method call by getForeignKeyName 1.2.0 (2019-03-07) ------------------ diff --git a/readme.md b/readme.md index 48397d2..7597a48 100644 --- a/readme.md +++ b/readme.md @@ -8,6 +8,7 @@ Includes some extensions/improvements to the Database section of Laravel Framewo - [Natural sort](#natural-sort) - [Default model sort](#default-model-sort) - [Joins using relationships](#joins-using-relationships) + - [Query builder whereLike macro](#query-builder-wherelike-macro) Installation ------------ @@ -157,3 +158,43 @@ class User extends Model } } ``` + +### Query builder whereLike macro + +Source : https://murze.be/searching-models-using-a-where-like-query-in-laravel + +A replacement of this: + +```php +User::query() + ->where('name', 'LIKE', "%{$searchTerm}%") + ->orWhere('email', 'LIKE', "%{$searchTerm}%") + ->get(); +``` + +By that: + +```php +User::whereLike(['name', 'email'], $searchTerm)->get(); +``` + +Or more advanced, a replacement of this: + +```php +Post::query() + ->where('name', 'LIKE', "%{$searchTerm}%") + ->orWhere('text', 'LIKE', "%{$searchTerm}%") + ->orWhereHas('author', function ($query) use ($searchTerm) { + $query->where('name', 'LIKE', "%{$searchTerm}%"); + }) + ->orWhereHas('tags', function ($query) use ($searchTerm) { + $query->where('name', 'LIKE', "%{$searchTerm}%"); + }) + ->get(); +``` + +By that: + +```php +Post::whereLike(['name', 'text', 'author.name', 'tags.name'], $searchTerm)->get(); +``` diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index a74ad39..d960826 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,8 +2,10 @@ namespace Axn\Illuminate\Database; -use Illuminate\Support\ServiceProvider as BaseServiceProvider; use Illuminate\Database\Connection; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\ServiceProvider as BaseServiceProvider; +use Illuminate\Support\Str; class ServiceProvider extends BaseServiceProvider { @@ -19,6 +21,38 @@ public function register() require __DIR__.'/query-builder-macros.php'; } + /** + * Bootstrap any application services. + * + * @return void + */ + public function boot() + { + // Add an Eloquent "whereLike" query builder macro + Builder::macro('whereLike', function ($attributes, $searchTerm) { + $searchTerm = str_replace(' ', '%', $searchTerm); + $this->where(function (Builder $query) use ($attributes, $searchTerm) { + foreach (array_wrap($attributes) as $attribute) { + $query->when(Str::contains($attribute, '.'), + function (Builder $query) use ($attribute, $searchTerm) { + list($relationName, $relationAttribute) = explode('.', $attribute); + + $query->orWhereHas($relationName, function (Builder $query) use ($relationAttribute, $searchTerm) { + $query->where($relationAttribute, 'LIKE', "%{$searchTerm}%"); + }); + }, + function (Builder $query) use ($attribute, $searchTerm) { + $query->orWhere($attribute, 'LIKE', "%{$searchTerm}%"); + } + ); + } + }); + + return $this; + }); + + } + /** * Replace MySqlConnection in the IoC by the extended one. * From 36949b907fe14404564bb73c99d558130d69b742 Mon Sep 17 00:00:00 2001 From: Vincent Garnier Date: Wed, 20 Mar 2019 14:24:31 +0100 Subject: [PATCH 7/7] Moving the "whereLike" macro to the right place --- changelog.md | 6 ++++++ src/ServiceProvider.php | 38 +----------------------------------- src/query-builder-macros.php | 26 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/changelog.md b/changelog.md index acb4caf..eb0f2d2 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,12 @@ Changelog ========= +2.1.1 (2019-03-20) +------------------ + +- Moving the "whereLike" macro to the right place +- Register macro in the boot() method instead of the register() one + 2.1.0 (2019-03-20) ------------------ diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index d960826..4bc8309 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -3,54 +3,18 @@ namespace Axn\Illuminate\Database; use Illuminate\Database\Connection; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\ServiceProvider as BaseServiceProvider; -use Illuminate\Support\Str; class ServiceProvider extends BaseServiceProvider { - /** - * Register any application services. - * - * @return void - */ public function register() { $this->replaceMySqlConnection(); - - require __DIR__.'/query-builder-macros.php'; } - /** - * Bootstrap any application services. - * - * @return void - */ public function boot() { - // Add an Eloquent "whereLike" query builder macro - Builder::macro('whereLike', function ($attributes, $searchTerm) { - $searchTerm = str_replace(' ', '%', $searchTerm); - $this->where(function (Builder $query) use ($attributes, $searchTerm) { - foreach (array_wrap($attributes) as $attribute) { - $query->when(Str::contains($attribute, '.'), - function (Builder $query) use ($attribute, $searchTerm) { - list($relationName, $relationAttribute) = explode('.', $attribute); - - $query->orWhereHas($relationName, function (Builder $query) use ($relationAttribute, $searchTerm) { - $query->where($relationAttribute, 'LIKE', "%{$searchTerm}%"); - }); - }, - function (Builder $query) use ($attribute, $searchTerm) { - $query->orWhere($attribute, 'LIKE', "%{$searchTerm}%"); - } - ); - } - }); - - return $this; - }); - + require __DIR__.'/query-builder-macros.php'; } /** diff --git a/src/query-builder-macros.php b/src/query-builder-macros.php index 2cc2de0..e9bd0f3 100644 --- a/src/query-builder-macros.php +++ b/src/query-builder-macros.php @@ -1,6 +1,8 @@ where(function (QueryBuilder $query) use ($attributes, $searchTerm) { + foreach (Arr::wrap($attributes) as $attribute) { + $query->when(Str::contains($attribute, '.'), + function (QueryBuilder $query) use ($attribute, $searchTerm) { + list($relationName, $relationAttribute) = explode('.', $attribute); + + $query->orWhereHas($relationName, function (QueryBuilder $query) use ($relationAttribute, $searchTerm) { + $query->where($relationAttribute, 'LIKE', "%{$searchTerm}%"); + }); + }, + function (QueryBuilder $query) use ($attribute, $searchTerm) { + $query->orWhere($attribute, 'LIKE', "%{$searchTerm}%"); + } + ); + } + }); + + return $this; +});