1

我正在为一个站点开发一个帮助中心,并且我已经在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。我已经对此进行了测试并且它有效。我可以插入fooand barinto Category 1and fooand barinto Category 2,但是如果我尝试将另一个添加fooCategory 1orCategory 2中,它不会验证,因为它是重复的,这是正确的。foo但是,如果我尝试更新它不会允许它,并且如果我尝试更新Category 1而不进行任何更改,它会忽略自身(行),允许它通过并且不会引发验证错误。因此,一切正常。barfoo

现在对于我的文章验证,我使用了与部分验证完全相同的过程/查询验证,除了更改表/列/变量以适应,如下所示:

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,因此在一个类别的每个部分中只允许一个文章标题的副本,同时允许一个标题相同但在一个类别的不同部分中的文章。该部分工作正常,但是如果我插入fooand barin Category 1 / Section 1,然后更新foobar它允许更改,对我来说,它不应该因为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.
4

1 回答 1

1

好吧,首先你的代码很难分析,如果我是你,我会使用数组语法:

   $rules = [
        'title' => ['required'. 'min:3'],
   ];
   $uniqueRule = 'unique:help_centre_categories';

   /* Update ------------------------------------- */
    if ($method == 'PATCH') {
       $uniqueRule.='title,'.$this->category->id
    }
    $rules['title'][] = $uniqueRule;

第二件事是$method变量。我不知道它来自哪里,但假设它具有正确的值,您可能应该以这种方式更改您的条件:

if (strtoupper($method) == 'PATCH' || strtoupper($method) == 'PUT') {
 ...
}

最后一件事,如果我尝试将一个Request类同时用于存储和更新,我通常会这样做:

$segments = $this->segments();
$id = intval(end($segments));
if ($id != 0) {
 // now you know it's update
}

如果您的方法不起作用,您可以尝试上述方法。

编辑

在您的Request对象内部,您可以使用getMethod()方法,例如:

public function rules() 
{
   $method = $this->getMethod();
   if ($method == 'PUT' || $method == 'PATCH') {
        // now you do what you want for update
   }

   // rest of code goes here
}

对于更新,您应该验证 $method 是PUTPATCH

于 2015-03-22T08:52:53.100 回答