DatabaseRule.php
TLDR
This file contains the DatabaseRule
trait, which provides methods for creating database validation rules. It includes methods for setting constraints on database queries, such as where clauses and custom query callbacks.
Methods
__construct
This method is the constructor of the DatabaseRule
trait. It initializes the $column
and $table
properties of the trait.
resolveTableName
This method resolves the name of the table from the given string. It checks if the table string is a valid class name, and if so, retrieves the table name from the corresponding model. It returns the resolved table name.
where
This method sets a "where" constraint on the query. It accepts the column and value as parameters. The value can be an Arrayable
, BackedEnum
, closure, array, string, integer, boolean, or null. It returns the instance of the DatabaseRule
trait.
whereNot
This method sets a "where not" constraint on the query. It accepts the column and value as parameters. The value can be an Arrayable
, BackedEnum
, array, or string. It returns the instance of the DatabaseRule
trait.
whereNull
This method sets a "where null" constraint on the query. It accepts the column as a parameter. It returns the instance of the DatabaseRule
trait.
whereNotNull
This method sets a "where not null" constraint on the query. It accepts the column as a parameter. It returns the instance of the DatabaseRule
trait.
whereIn
This method sets a "where in" constraint on the query. It accepts the column and values as parameters. The values can be an Arrayable
, BackedEnum
, or array. It returns the instance of the DatabaseRule
trait.
whereNotIn
This method sets a "where not in" constraint on the query. It accepts the column and values as parameters. The values can be an Arrayable
, BackedEnum
, or array. It returns the instance of the DatabaseRule
trait.
withoutTrashed
This method ignores soft deleted models during the existence check. It accepts the deletedAtColumn as a parameter, which defaults to 'deleted_at'. It returns the instance of the DatabaseRule
trait.
onlyTrashed
This method only includes soft deleted models during the existence check. It accepts the deletedAtColumn as a parameter, which defaults to 'deleted_at'. It returns the instance of the DatabaseRule
trait.
using
This method registers a custom query callback. It accepts a closure as a parameter. It returns the instance of the DatabaseRule
trait.
queryCallbacks
This method returns the array of custom query callbacks for the rule.
formatWheres
This method formats the where clauses. It returns the formatted string.
Classes
No classes are defined in this file.
<?php
namespace Illuminate\Validation\Rules;
use BackedEnum;
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
trait DatabaseRule
{
/**
* The table to run the query against.
*
* @var string
*/
protected $table;
/**
* The column to check on.
*
* @var string
*/
protected $column;
/**
* The extra where clauses for the query.
*
* @var array
*/
protected $wheres = [];
/**
* The array of custom query callbacks.
*
* @var array
*/
protected $using = [];
/**
* Create a new rule instance.
*
* @param string $table
* @param string $column
* @return void
*/
public function __construct($table, $column = 'NULL')
{
$this->column = $column;
$this->table = $this->resolveTableName($table);
}
/**
* Resolves the name of the table from the given string.
*
* @param string $table
* @return string
*/
public function resolveTableName($table)
{
if (! str_contains($table, '\\') || ! class_exists($table)) {
return $table;
}
if (is_subclass_of($table, Model::class)) {
$model = new $table;
if (str_contains($model->getTable(), '.')) {
return $table;
}
return implode('.', array_map(function (string $part) {
return trim($part, '.');
}, array_filter([$model->getConnectionName(), $model->getTable()])));
}
return $table;
}
/**
* Set a "where" constraint on the query.
*
* @param \Closure|string $column
* @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\Closure|array|string|int|bool|null $value
* @return $this
*/
public function where($column, $value = null)
{
if ($value instanceof Arrayable || is_array($value)) {
return $this->whereIn($column, $value);
}
if ($column instanceof Closure) {
return $this->using($column);
}
if (is_null($value)) {
return $this->whereNull($column);
}
if ($value instanceof BackedEnum) {
$value = $value->value;
}
$this->wheres[] = compact('column', 'value');
return $this;
}
/**
* Set a "where not" constraint on the query.
*
* @param string $column
* @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|array|string $value
* @return $this
*/
public function whereNot($column, $value)
{
if ($value instanceof Arrayable || is_array($value)) {
return $this->whereNotIn($column, $value);
}
if ($value instanceof BackedEnum) {
$value = $value->value;
}
return $this->where($column, '!'.$value);
}
/**
* Set a "where null" constraint on the query.
*
* @param string $column
* @return $this
*/
public function whereNull($column)
{
return $this->where($column, 'NULL');
}
/**
* Set a "where not null" constraint on the query.
*
* @param string $column
* @return $this
*/
public function whereNotNull($column)
{
return $this->where($column, 'NOT_NULL');
}
/**
* Set a "where in" constraint on the query.
*
* @param string $column
* @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|array $values
* @return $this
*/
public function whereIn($column, $values)
{
return $this->where(function ($query) use ($column, $values) {
$query->whereIn($column, $values);
});
}
/**
* Set a "where not in" constraint on the query.
*
* @param string $column
* @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|array $values
* @return $this
*/
public function whereNotIn($column, $values)
{
return $this->where(function ($query) use ($column, $values) {
$query->whereNotIn($column, $values);
});
}
/**
* Ignore soft deleted models during the existence check.
*
* @param string $deletedAtColumn
* @return $this
*/
public function withoutTrashed($deletedAtColumn = 'deleted_at')
{
$this->whereNull($deletedAtColumn);
return $this;
}
/**
* Only include soft deleted models during the existence check.
*
* @param string $deletedAtColumn
* @return $this
*/
public function onlyTrashed($deletedAtColumn = 'deleted_at')
{
$this->whereNotNull($deletedAtColumn);
return $this;
}
/**
* Register a custom query callback.
*
* @param \Closure $callback
* @return $this
*/
public function using(Closure $callback)
{
$this->using[] = $callback;
return $this;
}
/**
* Get the custom query callbacks for the rule.
*
* @return array
*/
public function queryCallbacks()
{
return $this->using;
}
/**
* Format the where clauses.
*
* @return string
*/
protected function formatWheres()
{
return collect($this->wheres)->map(function ($where) {
return $where['column'].','.'"'.str_replace('"', '""', $where['value']).'"';
})->implode(',');
}
}