Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lgtaxn committed Mar 20, 2019
2 parents b68e12b + 36949b9 commit 3b6009a
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 25 deletions.
31 changes: 29 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
Changelog for Laravel Database Extension
========================================
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)
------------------

- 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

1.2.0 (2019-03-07)
------------------

- Add support for Laravel 5.8

1.1.0 (2018-09-11)
------------------

- add support for Laravel 5.7

1.0.0 (2018-07-20)
------------------
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"license": "MIT",
"require": {
"laravel/framework": "5.4.*|5.5.*|5.6.*"
"laravel/framework": "5.8.*"
},
"autoload": {
"psr-4": {
Expand Down
57 changes: 42 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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)
- [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
------------
Expand All @@ -18,20 +19,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
-----

Expand Down Expand Up @@ -171,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();
```
2 changes: 1 addition & 1 deletion src/Eloquent/JoinRelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
];
}

Expand Down
10 changes: 4 additions & 6 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

namespace Axn\Illuminate\Database;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Illuminate\Database\Connection;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->replaceMySqlConnection();
}

public function boot()
{
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 3b6009a

Please sign in to comment.