有没有更好的方法来检查 Laravel 中 HasMany 中的强制性 child?
Is there a better way to check for mandatory child in HasMany in Laravel?
我有一个 class AdBannerModel
与 BannerLocalizedContentModel
有 HasMany
关系。在验证模型时,我调用了方法 alterRulesForSaving
,我想在其中检查给定的 AdBanner 是否至少具有强制性的 localizedContents。我现在就是这样处理支票的,求建议。
$linkTextPresent = false;
$linkUrlPresent = false;
$descriptionPresent = false;
/** @var BannerLocalizedContentModel $localizedContent */
foreach ($this->localizedContents() as $localizedContent) {
if ($localizedContent->type === BannerLocalizedContentModel::TYPE_LINK_URL) {
$linkUrlPresent = true;
}
if ($localizedContent->type === BannerLocalizedContentModel::TYPE_LINK_TEXT) {
$linkTextPresent = true;
}
if ($localizedContent->type === BannerLocalizedContentModel::TYPE_DESCRIPTION) {
$descriptionPresent = true;
}
}
if (!$linkUrlPresent || !$linkTextPresent || !$descriptionPresent) {
throw new ValidationErrorException(['content_i18n' => 'missing mandatory content.'], 'error.invalid_data');
}
您可以检查 existence 与所需参数的关系
// assuming you provide AdBannerModel code
// your HasMany relation
public function bannerLocalizedContentModels() {
return $this->hasMany(BannerLocalizedContentModel::class);
}
public function alterRulesForSaving(/*params if any*/) {
//...
$mandatoryContents = [
BannerLocalizedContentModel::TYPE_LINK_URL,
BannerLocalizedContentModel::TYPE_LINK_TEXT,
BannerLocalizedContentModel::TYPE_DESCRIPTION
];
if ($this->bannerLocalizedContentModels()->whereIn('type', $mandatoryContents)->doesntExist()) {
throw new ValidationErrorException(['content_i18n' => 'missing mandatory content.'], 'error.invalid_data');
}
}
我有一个 class AdBannerModel
与 BannerLocalizedContentModel
有 HasMany
关系。在验证模型时,我调用了方法 alterRulesForSaving
,我想在其中检查给定的 AdBanner 是否至少具有强制性的 localizedContents。我现在就是这样处理支票的,求建议。
$linkTextPresent = false;
$linkUrlPresent = false;
$descriptionPresent = false;
/** @var BannerLocalizedContentModel $localizedContent */
foreach ($this->localizedContents() as $localizedContent) {
if ($localizedContent->type === BannerLocalizedContentModel::TYPE_LINK_URL) {
$linkUrlPresent = true;
}
if ($localizedContent->type === BannerLocalizedContentModel::TYPE_LINK_TEXT) {
$linkTextPresent = true;
}
if ($localizedContent->type === BannerLocalizedContentModel::TYPE_DESCRIPTION) {
$descriptionPresent = true;
}
}
if (!$linkUrlPresent || !$linkTextPresent || !$descriptionPresent) {
throw new ValidationErrorException(['content_i18n' => 'missing mandatory content.'], 'error.invalid_data');
}
您可以检查 existence 与所需参数的关系
// assuming you provide AdBannerModel code
// your HasMany relation
public function bannerLocalizedContentModels() {
return $this->hasMany(BannerLocalizedContentModel::class);
}
public function alterRulesForSaving(/*params if any*/) {
//...
$mandatoryContents = [
BannerLocalizedContentModel::TYPE_LINK_URL,
BannerLocalizedContentModel::TYPE_LINK_TEXT,
BannerLocalizedContentModel::TYPE_DESCRIPTION
];
if ($this->bannerLocalizedContentModels()->whereIn('type', $mandatoryContents)->doesntExist()) {
throw new ValidationErrorException(['content_i18n' => 'missing mandatory content.'], 'error.invalid_data');
}
}