Skip to content

Commit

Permalink
Moving the "whereLike" macro to the right place
Browse files Browse the repository at this point in the history
  • Loading branch information
forxer committed Mar 20, 2019
1 parent 476c1b9 commit 36949b9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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)
------------------

Expand Down
38 changes: 1 addition & 37 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/query-builder-macros.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

/**
* Natural sorting.
Expand All @@ -25,3 +27,27 @@ function($column, $direction = 'asc') {
);
}
);

// Add an Eloquent "whereLike" query builder macro
QueryBuilder::macro('whereLike', function ($attributes, $searchTerm) {
$searchTerm = str_replace(' ', '%', $searchTerm);

$this->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;
});

0 comments on commit 36949b9

Please sign in to comment.