简体   繁体   中英

Laravel custom Request Validation for CameCase model

In custom GroupRequest in rules() I can get id of Group (that's the Model name) by $this->group->id.

public function rules()
{
    if ($this->method() == 'PATCH') {
        return [
            'name' => 'required|min:2|max:255|unique:groups,name,'.$this->group->id,
        ];
    } else {
        return [
            'name' => 'required|min:2|max:255|unique:groups',
        ];
    }
}

How can I do it in custom ArticleCategoryRequest... in rules() but id of ArticleCategory (that's the Model name)...

$this->articlecategory->id don't work.

It depends on the route variable you used.

If you declared your route like this :

Route::patch('article-categories/{articleCategory}', 'ArticleCategoryController@update');

You should be able to use $this->articleCategory in your ArticleCategoryRequest . And if there is implicit model binding you should be able to get the $this->articleCategory->id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM