Laravel 5 HTTP/Requests-将url参数传递给rules()方法 [英] Laravel 5 HTTP/Requests - pass url parameter to the rules() method

查看:252
本文介绍了Laravel 5 HTTP/Requests-将url参数传递给rules()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为slug字段在新的\HTTP\Requests\UpdateArticle类下创建一组规则,该规则需要应用unique过滤器,但仅当id不等于url参数时路由名称article/{article}的名称.

I'm trying to create a set of rules under the new \HTTP\Requests\UpdateArticle class for the slug field, which needs to have unique filter applied, but only when the id is not equal the url parameter of the route name article/{article}.

到目前为止,我得到的是:

What I got so far is:

public function rules()
{
    return [
        'title' => 'required|min:3',
        'excerpt' => 'required',
        'body' => 'required',
        'slug' => 'required|unique:articles,slug,?',
        'active' => 'required',
        'published_at' => 'required|date'
    ];

}

我需要用网址中的ID替换slug字段的唯一过滤器末尾的问号,但不知道如何获取它.

I need to replace the question mark at the end of the unique filter for slug field with the id from the url, but don't know how to obtain it.

推荐答案

您可以使用input()按名称检索路由参数:

You can retrieve route parameters by name with input():

$id = Route::input('id');
return [
    // ...
    'slug' => 'required|unique:articles,id,' . $id,
    // ...
];

文档中确实存在(向下滚动到访问路由参数值)

这篇关于Laravel 5 HTTP/Requests-将url参数传递给rules()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆