我正在为一个站点开发一个帮助中心,并且我已经在Laracast 论坛上提出了这个问题,但不幸的是,我还没有找到解决方案,这里是它的路由/url 路径:
路由.php
Route::resource('admin/help-centre/category', 'AdminHelpCentreCategoryController');
Route::resource('admin/help-centre/category/{category}/section', 'AdminHelpCentreSectionController');
Route::resource('admin/help-centre/category/{category}/section/{section}/article', 'AdminHelpCentreArticleController');
类别和部分验证按预期工作,但由于某种原因,即使它是相同的逻辑,据我所知,文章验证似乎没有按预期工作。这是我的验证规则(我删除了多余的部分,只留下了标题字段):
HelpCentreCategoryRequest.php
/* Insert ------------------------------------- */
$rules = [
'title' => 'required|min:3|unique:help_centre_categories',
];
/* Update ------------------------------------- */
if ($method == 'PATCH') {
$rules = [
'title' => 'required|min:3|unique:help_centre_categories,title,'.$this->category->id,
];
}
HelpCentreSectionRequest.php
/* Insert ------------------------------------- */
$rules = [
'title' => 'required|min:3|unique:help_centre_sections,title,NULL,id,category_id,'.$this->category->id,
];
/* Update ------------------------------------- */
if ($method == 'PATCH') {
$rules = [
'title' => 'required|min:3|unique:help_centre_sections,title,'.$this->section->id.',id,category_id,'.$this->category->id,
];
}
我将解释只是为了澄清......对于类别,INSERT
它会检查该标题是否已经存在,如果存在,它不会验证。UPDATE
完全一样,只是它在检查中忽略了自己,因此它仍然可以验证标题是否未更改。所以我输入了 2 个类别,一个叫做Category 1
,另一个是Category 2
.
Section 请求也是如此,除了它只检查您正在创建它的指定 Category 中的 Section。我已经对此进行了测试并且它有效。我可以插入foo
and bar
into Category 1
and foo
and bar
into Category 2
,但是如果我尝试将另一个添加foo
到Category 1
orCategory 2
中,它不会验证,因为它是重复的,这是正确的。foo
但是,如果我尝试更新它不会允许它,并且如果我尝试更新Category 1
而不进行任何更改,它会忽略自身(行),允许它通过并且不会引发验证错误。因此,一切正常。bar
foo
现在对于我的文章验证,我使用了与部分验证完全相同的过程/查询验证,除了更改表/列/变量以适应,如下所示:
HelpCenterArticleRequest.php
/* Insert ------------------------------------- */
$rules = [
'title' => 'required|min:3|unique:help_centre_articles,title,NULL,id,section_id,'.$this->section->id,
];
/* Update ------------------------------------- */
if ($method == 'PATCH') {
$rules = [
'title' => 'required|min:3|unique:help_centre_articles,title,'.$this->article->id.',id,section_id,'.$this->section->id,
];
}
这应该与 完全相同HelpCentreSectionRequest.php
,因此在一个类别的每个部分中只允许一个文章标题的副本,同时允许一个标题相同但在一个类别的不同部分中的文章。该部分工作正常,但是如果我插入foo
and bar
in Category 1 / Section 1
,然后更新foo
到bar
它允许更改,对我来说,它不应该因为bar
已经存在于Category 1 / Section 1
.
我显然错了,否则它也会像我预期的那样工作哈。但我就是看不出问题出在哪里?
这是数据库模式,只是为了节省任何麻烦并可以确认表/列名称匹配(再次删除外键和额外列):
Schema::create('help_centre_categories', function(Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('title');
});
Schema::create('help_centre_sections', function(Blueprint $table)
{
$table->increments('id')->unsigned();
$table->integer('category_id')->unsigned();
$table->string('title');
});
Schema::create('help_centre_articles', function(Blueprint $table)
{
$table->increments('id')->unsigned();
$table->integer('category_id')->unsigned();
$table->integer('section_id')->unsigned();
$table->string('title');
});
我还检查了if
文章请求中的声明肯定会触发,这是在顶部的 Laracast 论坛链接中提到的。您还可以在此处找到 Valiation 文档,但为了便于阅读,这是我使用的主要部分:
Adding Additional Where Clauses
You may also specify more conditions that will be added as "where" clauses to the query:
'email' => 'unique:users,email_address,NULL,id,account_id,1'
In the rule above, only rows with an account_id of 1 would be included in the unique check.