我正在开发一个小型应用程序,在Laravel 5.5
其中创建请求updateContact
并具有唯一的电子邮件验证规则,同时在控制器内部使用相同的验证,我可以轻松地进行:
$contact = Contact::find($request->id);
Validator::make($data, [
'first_name' => 'required|max:255',
'email' => 'required', Rule::unique('contacts')->ignore($contact->id),
'address' => 'max:255',
'city' => 'max:255',
'state' => 'max:255',
'country' => 'max:255',
]);
并且可以通过我提出请求php artisan make:request UpdateContact
并在里面添加以下内容来验证updateContact.php
:
namespace App\Http\Requests;
use App\Contact;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateContact extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$contact = Contact::find($request->id);
return [
'first_name' => 'required|max:255',
'email' => 'required', Rule::unique('contacts')->ignore($contact->id),
'company_id' => 'required',
'address' => 'max:255',
'city' => 'max:255',
'state' => 'max:255',
'country' => 'max:255',
];
}
public function messages()
{
return [
'company_id.required' => 'Company name is required'
];
}
}
但我不知道我该$request->id
如何使用它?