[英]Unique slug in editing with form request update method Laravel 5.4
Hey I'm having a hard time with this update method I have a slug field in my news model and it has to be unique, now when i update sometimes I may not want to change the slug but the validation of laravel doesnt let me send my data and when i bypass the validation Mysql won't let me insert into the slug field because of duplicate. 嘿,我很难使用这种更新方法,我的新闻模型中有一个slug字段,它必须是唯一的,现在当我更新时,有时我可能不想更改该slug,但是laravel的验证不允许我发送我的数据,以及当我绕过验证时,Mysql不会因为重复而将我插入子弹字段。 this is my update method and validation I have a handleRequest wich is just to validate images being uploaded.
这是我的更新方法,我有一个handleRequest,仅用于验证要上传的图像。
NewsController NewsController
public function update(CreateNewsRequest $request, $id)
{
$news = News::findOrFail($id);
$data = $this->handleRequest($request);
$news->update($data);
return redirect(route('admin.access.news.index'))->with('message', 'Your news has been updated');
}
CreateNewsRequest CreateNewsRequest
public function authorize()
{
//if(access()->hasRole(1) || access()->hasRole(2))
return true;
}
public function rules(){
$news = $this->route()->parameter('news');
$rules = [
'title' => 'required',
'slug' => 'required|unique:news',
'excerpt' => 'required',
'body' => 'required',
'category_id' => 'required',
'image' => 'mimes:gif,bmp,jpeg,jpg,png',
];
switch($this->method()){
case 'PUT':
case 'PATCH':
$rules['slug'] = 'required|unique:news,slug,' .$news;
break;
}
return $rules;
}
}
I put this function in NewsController for now 我现在将这个功能放在NewsController中
private function handleRequest($request)
{
if(! $request->published_at ){
$request->published_at = null;
}
$data = $request->all();
if($request->hasFile('image'))
{
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destination = public_path(config('cms.image.directory'));
$successUploaded = $image->move($destination, $fileName);
if($successUploaded)
{
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make($destination . '/' . $fileName)
->resize($width, $height)
->save($destination . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
You should ignore your current model by id
not by other fields that can be changed in update form. 您应该按
id
而不是可以通过更新形式更改的其他字段来忽略当前模型。 Try this: 尝试这个:
switch($this->method()){
case 'PUT':
case 'PATCH':
$id = $this->route('id');
$rules['slug'] = 'required|unique:news,slug,'.$id;
break;
}
@AndriyLozynskiy Editing works! @AndriyLozynskiy编辑工作! Turns out I had a mistake in the form thanks for the help.
原来我的表格有误,谢谢您的帮助。
I also changed the validation a bit and created an UpdateFormRequest.php 我还稍微更改了验证并创建了UpdateFormRequest.php
public function rules(Request $request){
$news = $this->route('news');
$rules = [
'title' => 'required',
'excerpt' => 'required',
'body' => 'required',
'category_id' => 'required',
'image' => 'mimes:gif,bmp,jpeg,jpg,png',
'slug' => ($request->slug != $this->slug) ? 'required|alpha_dash|min:5|max:255|unique:news,slug' : ''
];
return $rules;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.