我剛剛發現,當更新模型(比如說$post
模型)時,即更新正文文本和標題並點擊保存時,它會自動更新created_at列,但我希望它只更新updated_at列。Laravel 4更新模型並點擊保存時,只更新updated_at列
我已經搜索了很長時間才問,所以我知道$post->touch()
只更新了時間戳,而沒有更新。當在我的後期模型中設置protected $timestamp = false;
時,它僅更新/保存時禁用時間戳更新。
對於那些想知道的人,我目前仍在使用$post->save();
來更新帖子。
那麼,有沒有人知道如何在更新/編輯帖子時保留created_at
(將在創建帖子時設置)並更新updated_at
?
我之後的更新()控制器功能:
public function update($slug)
{
$post = Post::byslug($slug);
$post->concept = Input::get('concept');
$post->title = Input::get('title');
$post->slug = Str::slug(Input::get('title'));
$post->body = Input::get('body');
$post->metatitle = 'My Website |' . Input::get('title');
$post->metadescription = Input::get('body');
$post->robots = Input::get('robots');
$post->ogtitle = 'My Website |' . Input::get('title');
$post->ogdescription = Input::get('body');
$post->ogsitename = 'My Website';
$post->ogurl = Str::slug(Input::get('title'));
$post->ogtype = Input::get('ogtype');
if(Input::hasFile('file'))
{
$file = Input::file('file');
$destinationPath = 'uploads/images/posts/';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$post->image = str_replace('\\', '/', $upload_success);
$post->ogimage = str_replace('\\', '/', $upload_success);
}
if($post->save())
{
return Redirect::route('admin.blog.index')
->with('post', $post);
} else {
return Redirect::route('admin.blog.edit')
->with('post', $post)
->withInput();
}
}
究竟是什麼讓你認爲更新改變'created_at'列?它當然不會,所以顯示你的代碼和結果。 –
我檢查了我的數據庫並將'created_at'列更新爲今天的日期。所以它肯定是.. – Jeroen
就像我說的,如果您想要解決問題,請顯示代碼。 –