Laravel 弹头重复
Laravel Slug Duplicate
当我尝试使用相同的 slug 创建 post 时,它通常也会在更新时创建 post 同样的问题
在这种情况下,我为 post
复制了 slug
我搜索了很多关于这个问题但我没有得到任何答案
有解决这个问题的想法吗?
型号
protected $fillable = [
'title', 'slug', 'body',
];
控制器
public function slug($string, $separator = '-') {
if (is_null($string)) {
return "";
}
$string = trim($string);
$string = mb_strtolower($string, "UTF-8");;
$string = preg_replace("/[^a-z0-9_\sءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]#u/", "", $string);
$string = preg_replace("/[\s-]+/", " ", $string);
$string = preg_replace("/[\s_]/", $separator, $string);
return $string;
}
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|min:3|max:255|unique:posts',
'body' => 'required',
));
$post = new Post;
$post->title = $request->input('title');
$post->slug = $this->slug($request->slug);
$post->body = $request->input('body');
$post->save();
return redirect('admin/posts')->with('success', 'post is successfully saved');
}
public function update(Request $request, $id)
{
if ($request->isMethod('get'))
return view('content.admin.post.index', ['url' => Post::find($id)]);
else {
$rules = [
'title' => 'required|max:255',
'slug' => 'required|min:3|max:255|unique:posts,slug,{$post->slug},slug',
'body' => 'required',
];
$this->validate($request, $rules);
$post = Post::find($id);
$post->title = $request->title;
$post->slug = $this->slug($request->slug);
$post->body = $request->body;
$post->save();
return redirect('/admin/posts');
}
}
为什么不在数据库迁移中将 slug 列设置为唯一?
像这样:
$table->string('slug')->unique();
在将 slug 字段传递给验证器之前进行转换。
public function store(Request $request)
{
$request->slug = $this->slug($request->slug);
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|min:3|max:255|unique:posts',
'body' => 'required',
));
$post = new Post;
$post->title = $request->input('title');
$post->slug = $request->input('slug');
$post->body = $request->input('body');
$post->save();
return redirect('admin/posts')->with('success', 'post is successfully saved');
}
当我尝试使用相同的 slug 创建 post 时,它通常也会在更新时创建 post 同样的问题 在这种情况下,我为 post
复制了 slug我搜索了很多关于这个问题但我没有得到任何答案
有解决这个问题的想法吗?
型号
protected $fillable = [
'title', 'slug', 'body',
];
控制器
public function slug($string, $separator = '-') {
if (is_null($string)) {
return "";
}
$string = trim($string);
$string = mb_strtolower($string, "UTF-8");;
$string = preg_replace("/[^a-z0-9_\sءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]#u/", "", $string);
$string = preg_replace("/[\s-]+/", " ", $string);
$string = preg_replace("/[\s_]/", $separator, $string);
return $string;
}
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|min:3|max:255|unique:posts',
'body' => 'required',
));
$post = new Post;
$post->title = $request->input('title');
$post->slug = $this->slug($request->slug);
$post->body = $request->input('body');
$post->save();
return redirect('admin/posts')->with('success', 'post is successfully saved');
}
public function update(Request $request, $id)
{
if ($request->isMethod('get'))
return view('content.admin.post.index', ['url' => Post::find($id)]);
else {
$rules = [
'title' => 'required|max:255',
'slug' => 'required|min:3|max:255|unique:posts,slug,{$post->slug},slug',
'body' => 'required',
];
$this->validate($request, $rules);
$post = Post::find($id);
$post->title = $request->title;
$post->slug = $this->slug($request->slug);
$post->body = $request->body;
$post->save();
return redirect('/admin/posts');
}
}
为什么不在数据库迁移中将 slug 列设置为唯一? 像这样:
$table->string('slug')->unique();
在将 slug 字段传递给验证器之前进行转换。
public function store(Request $request)
{
$request->slug = $this->slug($request->slug);
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|min:3|max:255|unique:posts',
'body' => 'required',
));
$post = new Post;
$post->title = $request->input('title');
$post->slug = $request->input('slug');
$post->body = $request->input('body');
$post->save();
return redirect('admin/posts')->with('success', 'post is successfully saved');
}