【发布时间】:2023-04-06 16:53:01
【问题描述】:
我有这个表单请求如下:
<?php
namespace App\Http\Requests;
use App\Sociallink;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class SociallinkRequest 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()
{
return [
'seq' => 'required|unique:sociallinks,seq,' . $this->id . ',id',
'social_name' => 'required|unique:sociallinks,social_name,' . $this->id . ',id',
'cssclass' => 'required',
'url' => 'nullable|active_url'
];
}
我需要字段 seq 和 social_name 是唯一的。当我尝试编辑时,此代码不起作用。我发现$this->id 不存在于dd($this)。我的网址是:http://prj.test/sociallink/2/edit。这里的许多示例都使用了$this->id,但我无法在代码中的任何位置访问该变量,因为它似乎不存在。当我在本例中将$this->id 替换为物理id(如2)时,验证工作正常。但是动态地,我如何使用当前行的id 来做唯一验证?
【问题讨论】:
标签: laravel validation laravel-5 request unique