Krystus
Krystus

Reputation: 315

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.

Upvotes: 0

Views: 113

Answers (1)

SimonDepelchin
SimonDepelchin

Reputation: 2080

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.

Upvotes: 1

Related Questions