针对唯一值的Laravel验证



我想为更新函数创建一个验证我希望它排除该dedant id 的标题

public function update(Request $request, Post $post)
{
$this->validate($request,[
'title' => "required|unique:posts,title,".$id.'id',
'image' => 'image',
'categories' => 'required',
//'tags' => 'required',
'body' => 'required',
]);

我提出了另一个有效的例子,也许它可以派上的用场

public function update(Request $request, $id)
{
$this->validate($request,[
'name' => "required|unique:categories,name,".$id.'id',
'image' => 'mimes:jpeg,bmp,png,jpg'
]);

这个代码可以工作,

public function update(Request $request, Post $post)
{
$this->validate($request,[
'title' => ["required,unique:posts,title,".$this->route('post')],
'image' => 'image',
'categories' => 'required',
//'tags' => 'required',
'body' => 'required',
]);

让我知道这对你有用吗。

你可以试试这个:

public function update(Request $request, $id)
{
$rules = [
'title' => 'required|unique:posts,title,'.$id,
'image' => 'image',
'categories' => 'required',
'body' => 'required',
];
$this->ValidateForm($request->all(), $rules);
$post= Post::find($id);  
if($post){
$post->title  = $request->title;
$post->categories  = $request->categories;
$post->body  = $request->body;
if ($request->hasFile('image')){
Storage::delete($request->profile_photo);
$post->image = $request->file('image')->store('post');
}
$post->save();
}
}

相关内容

  • 没有找到相关文章

最新更新