在我的update
方法中,我FormRequest
用来验证输入字段。我还需要为unique
验证规则传递和附加参数。
我正在尝试$subscriber
从url
.
订阅者控制器.php
class SubscriberController extends Controller
{
public function update(UpdateSubscriberRequest $request, $subscriber)
{
...
}
}
更新订阅者请求.php
class UpdateSubscriberRequest extends FormRequest
{
private $data = [];
private $subscriber = null;
public function __construct()
{
$this->data = request()->all();
$this->subscriber = request()->route('subscriber');
}
public function rules()
{
return [
...
'email' => [
'required',
...
"unique:subscribers,email,{$this->subscriber},token,company_id,"
. session()->get('COMPANY_ID')
],
'mobile' => [
'required',
...
"unique:subscribers,mobile,{$this->subscriber},token,company_id,"
. session()->get('COMPANY_ID')
],
...
];
}
}
虽然代码工作正常。但是,这是正确的方法吗..?