2013-09-20 38 views
0

任何最好的辦法,這將是gratefull最好的方式使用資源和雄辯laravel 4

該做的是什麼樣的一種形式搶輸入,並將其保存到數據庫中這樣做IM。

public function update() 
{ 

    $file = Input::file('path'); 

$destinationPath = 'img/'; 
$filename = $file->getClientOriginalName(); 
// $extension =$file->getClientOriginalExtension(); 
$upload_success = Input::file('path')->move($destinationPath, $filename); 
$photo = Photo::find($_POST['id']); 
    $photo->caption = $_POST['caption']; 
    $photo->path = $destinationPath . $filename; 
    $photo->save(); 


if($upload_success) { 
return Redirect::to('photos/'.$_POST['id'].'/edit')->withInput()->with('success', 'Photo  have been updated.'); 
} else { 
return Response::json('error', 400); 
} 
} 

這項工作得很好,但我不知道是否有一個簡化的方法來做到這一點怎麼樣我可以從表單POST數據發送更新來更新照片信息,而不是我的使用$ _ POST,並獲得id從表單解析到更新($ id)等。謝謝

+0

你會在你的代碼之前編輯你的問題的主體,這實際上是做什麼的? – halfer

回答

0

您可以使用Input類,而不是直接訪問帖子。

我可能會重新寫的功能有點像這樣:

public function update() 
{ 
    $file = Input::file('path'); 
    $destinationPath = 'img/'; 
    $filename = $file->getClientOriginalName(); 

    if(Input::file('path')->move($destinationPath, $filename)) 
    { 
     $photo = Photo::find(Input::get('id')); 
     $photo->caption = Input::get('caption'); 
     $photo->path = $destinationPath . $filename; 
     $photo->save(); 
     return Redirect::to('photos/'.$_POST['id'].'/edit')->withInput()->with('success', 'Photo  have been updated.'); 
    } 
    else 
    { 
     return Response::json('error', 400); 
    } 
} 

另一種選擇是提取某些數據的直接到你的照片模式,做在那裏。

+0

謝謝你,我還在沉重的學習,我更喜歡laraval:D –