43 lines
1008 B
PHP
43 lines
1008 B
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ExistInSchema implements ValidationRule
|
|
{
|
|
protected $schema;
|
|
protected $table;
|
|
protected $column;
|
|
|
|
public function __construct($schema, $table, $column)
|
|
{
|
|
$this->schema = $schema;
|
|
$this->table = $table;
|
|
$this->column = $column;
|
|
}
|
|
|
|
public function passes($attribute, $value)
|
|
{
|
|
// Check if a record exists with the given value in the specified schema, table, and column.
|
|
$query = DB::table($this->schema . '.' . $this->table)
|
|
->where($this->column, $value);
|
|
|
|
return $query->exists();
|
|
}
|
|
|
|
public function message()
|
|
{
|
|
return 'The selected :attribute is invalid.';
|
|
}
|
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
if (!$this->passes($attribute, $value)) {
|
|
$fail($this->message());
|
|
}
|
|
}
|
|
}
|