下面這個控制器裡面是一個樣本,它的圖片是可有可無的,所以是把圖片單獨分出來處理。現在想改一下,就是圖片必須上傳,那麼可以把圖片和文本一起$request->all()嗎?怎麼修改代碼?
public function store(ArticleRequest $request){ $article = new Article($request->except('image')); $article -> user_id = Auth::id(); $picture = ""; if(Input::hasFile('image')) { $file = Input::file('image'); $filename = $file->getClientOriginalName(); $extension = $file -> getClientOriginalExtension(); $picture = sha1($filename . time()) . '.' . $extension; } $article -> picture = $picture; $article -> save(); if(Input::hasFile('image')) { $destinationPath = public_path() . '/images/article/'.$article->id.'/'; Input::file('image')->move($destinationPath, $picture); }}
下面是視圖:
回複內容:
下面這個控制器裡面是一個樣本,它的圖片是可有可無的,所以是把圖片單獨分出來處理。現在想改一下,就是圖片必須上傳,那麼可以把圖片和文本一起$request->all()嗎?怎麼修改代碼?
public function store(ArticleRequest $request){ $article = new Article($request->except('image')); $article -> user_id = Auth::id(); $picture = ""; if(Input::hasFile('image')) { $file = Input::file('image'); $filename = $file->getClientOriginalName(); $extension = $file -> getClientOriginalExtension(); $picture = sha1($filename . time()) . '.' . $extension; } $article -> picture = $picture; $article -> save(); if(Input::hasFile('image')) { $destinationPath = public_path() . '/images/article/'.$article->id.'/'; Input::file('image')->move($destinationPath, $picture); }}
下面是視圖:
做非同步上傳可好?