我遇到了一个实例,我需要更改需要验证的数据,即当没有提交 slug 时,从标题创建一个,然后验证它是唯一的。
该请求有一个方法replace()
应该替换请求中的输入数据,但它似乎不起作用。任何人都可以对此有所了解吗?这就是我所拥有的:
<?php namespace Purposemedia\Pages\Http\Requests;
use Dashboard\Http\Requests\Request;
use Illuminate\Auth\Guard;
class PageRequest extends Request {
/**
* [authorize description]
* @return {[type]} [description]
*/
public function authorize( Guard $auth) {
return true;
}
/**
* [rules description]
* @return {[type]} [description]
*/
public function rules() {
// Get all data from request
$data = $this->request->all();
if( $data['slug'] === '' ) {
// if the slug is blank, create one from title data
$data['slug'] = str_slug( $data['title'], '-' );
// replace the request data with new data to validate
$this->request->replace( $data );
}
return [
'title' => 'required|min:5|max:255',
'slug' => 'min:5|max:255|alpha_dash|unique:pages,slug' . $this->getSegmentFromEnd(),
];
}
}