15

我目前有一个具有文本字段和 slug 字段的模型。

我验证 slug 在我的表单请求类中是唯一的:

public function rules()
{
    return [
        'name' => 'required|min:3',
        'slug' => 'required|alpha_dash|unique:questions'
    ];
}

这在创建时可以正常工作,并且可以正确地拒绝创建重复的 slug。但是在我的更新方法中,它不会让我保存记录,因为 slug 已经存在。当然 slug 确实存在,但它存在于正在编辑的记录中,所以我想继续允许它被保存。但是,它不应该被更改为 ANOTHER 记录上的 slug。

这是我的更新 ArticlesController 方法的样子:

public function update(Article $article, ArticleRequest $request)
{
    $article->update($request->all());

    return redirect('articles');
}

有没有办法在 L5 中完成这项工作?

4

6 回答 6

7

尝试修改您的规则,如下所示(在表单请求类中):

public function rules()
{
    return [
      'name'  => 'required,min:3',
      'slug'  => 'required|alpha_dash|unique:categories,slug,'.$this->id')
    ];
}

这个对我有用。

于 2015-08-28T17:25:58.193 回答
4

唯一规则中,您可以指定要忽略的 id。

您可以创建 2 个单独的请求(一个用于创建,一个用于更新),但您也可以通过这种方式检查是否已设置(我假设您的更新 url 看起来像/questions/2):

public function rules()
{
    $rules = [
        'name' => 'required|min:3',
        'slug' => ['required', 'alpha_dash']
    ];

    $rule = 'unique:questions';

    $segments = $this->segments();
    $id = intval(end($segments));
    if ($id != 0) {  
         $rule .= ',slug,' . $id;
    }
    $rules['slug'][] = $rule;

    return $rules;
    }
}
于 2015-02-22T21:16:19.503 回答
2

如果你必须有更新 slug 的能力,我做过的项目通常要求它在创建后不可编辑,那么你可以使用 laravel 的内置规则通过主键忽略表上的某个记录。

$rules['slug'] = "required|unique:questions,slug,{$id}";

http://laravel.com/docs/5.0/validation 请参阅“强制使用唯一规则忽略给定 ID”

于 2015-02-22T21:30:27.660 回答
2

In EditArticleRequest:

public function $rules () 
{
    $id = $this->id;

    return [
        'name' => 'required|min:3',
        'slug' => "required|alpha_dash|unique:articles,slug,$id",
    ];
}
于 2017-03-20T11:07:27.740 回答
1

以下是我在 Laravel 5.3 中的详细操作方法:

1-通过在终端中执行下一个命令来创建一个新的表单请求类:

php artisan make:request ArticleFormRequest

ArticleFormRequest表单请求类的名称在哪里。ArticleFormRequest.php该命令将在目录中创建一个名为的app/Http/Requests文件。

2-打开该创建的文件并删除其内容,然后将下一个内容放入其中:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Article;

class ArticleFormRequest extends FormRequest
{
    protected $rules = [
        'name' => 'required|min:3',
        'slug' => 'required|alpha_dash|unique:articles,slug',
    ];

    // protected $user; // in case you want the current authenticated user
    protected $request_method;
    protected $id;

    public function __construct(Request $request)
    {
        // $request->user() returns an instance of the authenticated user
        // $this->user = $request->user(); // in case you want the current authenticated user

        // $request->method() returns method of the request (GET, POST, PUT, DELETE, ...)
        $this->request_method = strtoupper($request->method());

        // segments(): Returns an array containing all of the segments for the request path
        // it is important to assign the returned "segments" array to a variable first before using it, otherwise an error will occur
        $segments = $request->segments();
        // note this way will be valid only if "id" of the element is the last segment
        $this->id = end($segments);
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = $this->rules;

        if ($this->request_method == "POST") {
            // do nothing..
        } elseif (in_array($this->request_method, ["PUT", "PATCH"])) {
            $article = Article::find($this->id);

            if ($article) {
                // forcing a unique rule to ignore a given id | https://laravel.com/docs/5.3/validation
                $rules["slug"] = [
                    "required",
                    "alpha_dash",
                    Rule::unique("articles", "slug")->ignore($article->id, "id"),
                ];

                // this is also can be used
                // $rules['slug'] = "required|alpha_dash|unique:articles,slug,$article->id,id";
            }
        }

        return $rules;
    }
}

3-在您的控制器中,您可以使用它和这样ArticleFormRequest的方法:store()update()

<?php

namespace App\Http\Controllers;

use App\Http\Requests\ArticleFormRequest;

class ArticlesController extends Controller
{


    public function store(ArticleFormRequest $request)
    {
        // your code here..
    }

    public function update(ArticleFormRequest $request, $id)
    {
        // Your code here..
    }

}
于 2016-12-21T14:14:26.753 回答
0

如前所述,您可以在验证器功能中使用忽略功能。

只需引用您希望忽略的项目的 ID,并确保在更新时使用补丁请求!

在这里查看更多信息!http://laravel.com/docs/5.0/validation#rule-unique

protected $rules = [    
    'name' => 'required|min:3',
    'slug' => 'required|alpha_dash|unique:questions'
];

public function rules()
{
    $rules = $this->rules;
    if ($this->isMethod('patch')) 
    {
        $id = $this->articles;
        $rules['slug'] = $rules['slug'].',slug,'.$id;
    }
    return $rules;
}
于 2015-08-01T00:50:49.560 回答