gpt4 book ai didi

php - 编辑时唯一的 key 违规

转载 作者:行者123 更新时间:2023-11-29 23:46:36 25 4
gpt4 key购买 nike

我正在尝试更新一篇博客文章,但我从数据库部分收到了唯一的关键错误,然后我没有使用模型并直接访问 ORM,但又没有成功。

这是我具体要编辑的路线

Route::get('/getedit/{slug}', array('as' => 'getedit', 'uses' => 'AdminController@getEdit'))->before('auth');
Route::post('/postedit', array('as' => 'postedit', 'uses' => 'AdminController@postEdit'))->before('auth');

Controller

public function getEdit($slug)
{
$article = Post::where('slug', '=' , $slug)
->firstOrFail();

return View::make('admin.edit', array(

'title' => $article->title,
'mainarticle' => $article->article,
'slug' => $article->slug,
'category' => $article->category

));
}

// Updates articles to database
public function postEdit()
{
$rules = [
'title' => 'required',
'article' => 'required',
'slug' => 'required|unique:posts,slug,9',
'category' => 'required'
];

$input = Input::all();

$validator = Validator::make($input, $rules);

if ($validator->fails()) {
return Redirect::route('getedit')
->withErrors($validator);
// withInput not defined
}
else
{
$slug = $input['slug'];
/*$affectedRows = Post::where('slug', '=', $slug)->update([
'title' => $input['title'],
'article' => $input['article'],
'slug' => $input['slug'],
'category' => $input['category']
]);*/

/*$affectedRows = Post::where('slug', '=', $slug)->firstOrFail();
$affectedRows->title = $input['title'];
$affectedRows->article = $input['article'];
$affectedRows->slug = $input['slug'];
$affectedRows->category = $input['category'];

$affectedRows->save();*/

$post = DB::table('posts')->where('slug', '=', $slug)->update([
'title' => $input['title'],
'article' => $input['article'],
'slug' => $input['slug'],
'category' => $input['category']
]);


if ($post) {
return Redirect::route('dashboard')
->with('flash_message','Article Successfully Inserted');
}
else
{
return Redirect::route('dashboard')
->with('flash_message','Error updating data');
}

}
}

我的模型只是创建数据库对象(我不小心遵循了胖 Controller 和瘦模型方法,因为我只是尝试框架)。我尝试过使用 Post::find(1)->update($data); 方法,但这会返回唯一的违规行为,而我当前的方法只是执行更新失败时触发的 else 语句。

注意:我是 Laravel 新手,第一次尝试这个。

最佳答案

当您更新帖子时,您宁愿发送 POST (或更好的 PATCH/PUT- http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html )请求到给定资源。

也就是说,您可以在网址中包含编辑后的行键,并将方法更改为如下所示:

// route
Route::post('/postedit/{id}', array('as' => 'postedit', 'uses' => 'AdminController@postEdit'))
->before('auth');

// controller
public function postEdit($id)
{
// if no posts with $id found, throws exception - catch it and eg. show 404
$post = Post::findOrFail($id);

$rules = [
'title' => 'required',
'article' => 'required',
'slug' => 'required|unique:posts,slug,'.$id, // to ignore this row in unique check
'category' => 'required'
];

// validate

$post->fill($input)->save(); // fill() in order to use mass-assignement check

// alternatively you can just update:
// $post->update($input);
// but then make sure $input has only elements corresponding to the table columns

此外,请阅读 route grouping ,因此您无需单独向这些路由添加 before('auth')

关于php - 编辑时唯一的 key 违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25899497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com