-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[6.x] Allow explicit Model definitions in database rules #30653
Conversation
Does it make sense to extend on this and automatically determine which column to search through (if none is provided) based off of the model's primary key? |
} | ||
|
||
return $model->getTable(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel this could have been cleaner by checking the opposite, i.e. checking if the argument is a Model
instance rather than not; checking the string is a class and exists rather than not.
public function resolveTableName($table)
{
if (is_object($table) && $table instanceof Model) {
return $table->getTable();
}
if (class_exists($table)) {
return (new $table)->getTable();
}
return $table;
}
Does this work with the fluent rule builders too, i.e. |
@ahinkle great addition but think it should also be using |
Unless I am missing something, I don't believe this PR is covering every possible case. Keeping in mind that my models are separated into the use App\Models\User; Option 1: validation rules as a single | delimited stringInput: $request->validate([
'alias' => 'string|unique:'.User::class,
]); Output: Option 2: validation rules as an array of stringsInput: $request->validate([
'alias' => ['string', 'unique:'.User::class],
]); Output: Option 3: validation rules as an array of Rule instancesInput: use Illuminate\Validation\Rule;
$request->validate([
'alias' => ['string', Rule::unique(User::class)],
]); Output: I believe that this is because when using parsed validation rules, the flow of validation does not at any stage create new instances of |
This PR creates the ability to explicitly define models within Database Rule; allowing to pass models to the
exists
andunique
validation rules.I've always found it a bit strange when I have to write out the database table name within my validation rules when the framework will give you a hand in most other places (such as eloquent relationships).
This will also prevent issues when you are changing up the table name and you don't have to go through each and every
exists
andunique
rule to update the table name.