Skip to content

Commit

Permalink
feat(DB): Macros for null or empty dealings
Browse files Browse the repository at this point in the history
  • Loading branch information
craigAtCD committed Jul 12, 2021
1 parent 9816185 commit 15c3e8e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Laravel Helpers
# Laravel Helpers <!-- no toc -->

Collection of helpers for re-use accross a few of our projects


- [Installation](#installation)
- [Crud Policy Trait](#crud-policy-trait)
- [Helpers](#helpers)
- [Delayed notifications blocking:](#delayed-notifications-blocking)
- [DB Macros](#db-macros)
- [Credits](#credits)

## Installation

Install via composer
Expand Down Expand Up @@ -96,6 +104,18 @@ class TaskReminder extends Notification implements ShouldQueue

By listening to that event and then checking our `blockSending()` method on our notification, we can do in-the-moment checks as the notification is being handled to see if it’s still valid. You can even access the `$notifiable` object, which allows you to check fresh data about the user or entity you’re notifying.


## DB Macros

### Null Or Empty
when dealing with some of our legacy databases we have some columns where the entry is either null or empty and these macros allow you to query this without the double entries:

```php
Model::whereNullOrEmpty('column_name'); //generates select * where 1=1 and (column_name is null or column_name = '')
Model::orWhereNullOrEmpty('column_name'); //generates select * where 1=1 or (column_name is null or column_name = '')
Model::whereNotNullOrEmpty('column_name'); //generates select * where 1=1 and (column_name is not null and column_name != '')
Model::orWhereNotNullOrEmpty('column_name'); //generates select * where 1=1 or (column_name is not null and column_name != '')

## Credits

- [](https://github.com/custom-d/laravel-helpers)
Expand Down
44 changes: 44 additions & 0 deletions src/Database/Query/Mixins/NullOrEmptyMixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace CustomD\LaravelHelpers\Database\Query\Mixins;

use Illuminate\Database\Query\Builder;

/** @mixin \Illuminate\Database\Query\Builder */

class NullOrEmptyMixin
{

public function whereNullOrEmpty()
{
return function (string $column) {
/** @var \Illuminate\Database\Query\Builder $this */
return $this->where(fn (Builder $builder) => $builder->where($column, '=', '')->orWhereNull($column));
};
}

public function orWhereNullOrEmpty()
{
return function (string $column) {
/** @var \Illuminate\Database\Query\Builder $this */
return $this->orWhere(fn (Builder $builder) => $builder->where($column, '=', '')->orWhereNull($column));
};
}


public function whereNotNullOrEmpty()
{
return function (string $column) {
/** @var \Illuminate\Database\Query\Builder $this */
return $this->where(fn(Builder $builder) => $builder->where($column, '!=', '')->whereNotNull($column));
};
}

public function orWhereNotNullOrEmpty()
{
return function (string $column) {
/** @var \Illuminate\Database\Query\Builder $this */
return $this->orWhere(fn(Builder $builder) => $builder->where($column, '!=', '')->orWhereNotNull($column));
};
}
}
8 changes: 8 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace CustomD\LaravelHelpers;

use Illuminate\Support\Facades\Event;
use Illuminate\Database\Query\Builder;
use CustomD\LaravelHelpers\Database\Query\Mixins\NullOrEmptyMixin;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
Expand All @@ -12,6 +14,7 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
public function boot()
{
$this->registerEvents();
$this->registerDbMacros();
}

public function register()
Expand All @@ -26,4 +29,9 @@ protected function registerEvents()
}
}
}

protected function registerDbMacros()
{
Builder::mixin(new NullOrEmptyMixin());
}
}

0 comments on commit 15c3e8e

Please sign in to comment.