我有这个表格来编辑文章和更改照片:
<h1>Edit: {{$article->title}}</h1>
<hr>
{!! Form::model($article, ['method'=>'PATCH','files' => true, 'action'=>['ArticlesController@update', $article->id]]) !!}
@include('articles.form',['submitButtonText'=>'Update Article'])
{!! Form::close() !!}
@include('errors.list')
现在在Controller我有这个功能:
public function update($id, Requests\ArticleRequest $request)
{
$photo= 'http://nationaluasi.com/dru/content/hotelIcon.png';
$file = array('photo' => $request->file('photo'));
// setting up rules
$rules = array('photo' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
// doing the validation, passing post data, rules and the messages
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
// send back to the page with the input data and errors
$photo = 'http://nationaluasi.com/dru/content/hotelIcon.png';
}
else {
// checking file is valid.
if ($request->file('photo')->isValid()) {
$destinationPath = public_path().'/images'; // upload path
$extension = $request->file('photo')->getClientOriginalExtension(); // getting image extension
$photo = str_random(5).'.'.$extension; // renameing image
$request->file('photo')->move($destinationPath, $photo); // uploading file to given path
// sending back with message
}
else {
}
}
$article = Auth::user()->articles()->findOrFail($id);
$article['photo'] = $photo;
$article->update($request->all());
Alert::message('Your auction is updated', 'Wonderful!');
return redirect('auctions');
}
但是现在当我尝试提交上传的照片时,我会在photo
列C:\wamp\tmp\php21F4.tmp
中得到此结果,但图片也会上传到/images
文件夹...
这是什么问题?如何更新文章......我想说的是,当我添加文章时,一切都很好 - 添加照片等方法store
一切都一样,工作正常......
更新
我试试:
$article = Auth::user()->articles()->findOrFail($id);
$article['photo'] = $photo;
dd($photo);
一切都很好,照片上传成功只是我没有更新文章[&#39;照片&#39;] ...所以问题在这里:
$物品─&GT;更新($请求 - &GT;所有());
但是如何解决呢?为什么文章[&#39;照片&#39;]未更新?答案 0 :(得分:1)
我试图纠正你的代码,使用它。
public function update($id, Requests\ArticleRequest $request)
{
$this->validate($request, [
'photo' => 'required|image|max:10000',
// validate also other fields here
]);
// checking file is valid.
if (!$request->file('photo')->isValid()) return redirect()->back()->withErrors(["photo" => "File is corrupt"]);
// file is valid
$destinationPath = public_path().'/images'; // upload path
$extension = $request->file('photo')->getClientOriginalExtension(); // getting image extension
$filename = str_random(5).'.'.$extension; // give a name to the image
$request->file('photo')->move($destinationPath, $filename); // uploading file to given path
// sending back with message
$article = Auth::user()->articles()->findOrFail($id); //if article id is unique just write Article::findOrFail($id)
$article_fields = $request->except('photo');
$article_fields['photo'] = $filename;
$article->update($article_fields);
Alert::message('Your auction is updated', 'Wonderful!');
return redirect('auctions');
}
答案 1 :(得分:1)
我假设您正在使用Laravel 5.我相信问题就在于这一行:
$article->update($request->all());
update方法需要一组列和值对。您上面的代码正在为其提供原始(未经修改的)请求,但不包括您的新照片位置。
使用Laravel,您实际上可以获取文章对象,直接在对象中设置列,然后保存更改。
尝试将方法的最后几行更改为:
$article = Auth::user()->articles()->findOrFail($id); // get the article
$article->photo = $photo; // set the photo column to your new location
$article->save(); // this will save your changes
Alert::message('Your auction is updated', 'Wonderful!');
return redirect('auctions');
请查看Laravel documentation以获取更多解释。