Skip to content

Instantly share code, notes, and snippets.

@FatBoyXPC
Last active August 3, 2017 14:24
Show Gist options
  • Select an option

  • Save FatBoyXPC/b99c0c1f545dc6e12129becf5d0970b9 to your computer and use it in GitHub Desktop.

Select an option

Save FatBoyXPC/b99c0c1f545dc6e12129becf5d0970b9 to your computer and use it in GitHub Desktop.
<?php
namespace App\Traits\Database;
trait MaybeTrait
{
/**
* I found myself doing something similar to this:
* $results = Company::where('type', $type)
*
* if ($region) {
* $results->where('region', $region)
* }
*
* $results->get();
*
* But that lends itself to the when() method:
*
* Company::where('type', $type)
* ->when($region, function ($query) {
* $query->where('region', $region);
* })
* ->get();
*
* However, I'm not a fan of that boilerplate for every "maybe". With this
* trait, we can instead do this:
*
* Company::where('type', $type)
* ->maybeWhere($region, 'region', $region)
* ->get();
*
* Where the first param when calling a "maybe" method is a predicate
* function.
*/
protected function maybeScope($query, $predicate, $method, $args)
{
return $query->when($predicate, function ($q) use ($method, $args) {
return $q->{$this->maybeMethodName($method)}(...$args);
});
}
protected function maybeMethodName($method)
{
return lcfirst(str_replace('scopeMaybe', '', $method));
}
public function scopeMaybeWhere($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhere($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereIn($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereIn($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereNotIn($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereNotIn($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereExists($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereNotExists($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereExists($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereNotExists($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereBetween($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereNotBetween($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereBetween($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereNotBetween($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereNull($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereNotNull($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereNull($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereNotNull($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereColumn($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereColumn($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereDate($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereTime($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereDate($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeOrWhereTime($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereDay($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereMonth($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
public function scopeMaybeWhereYear($query, $predicate, ...$args)
{
return $this->maybeScope($query, $predicate, __FUNCTION__, $args);
}
}
@davidjeddy
Copy link

  1. Point taken. Sadly not that familiar with Laravel's query engine :(.
  2. Well stated.
  3. :/ Wish Yii did this...
  4. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment