我有这个表格来编辑文章和更改照片:
<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')
现在在控制器我有这个功能:
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);
一切都很好,照片上传成功只是我没有更新文章[照片]。。。所以问题就在这里:
$article->update($request->all());
但是如何解决呢?为什么文章[照片]没有更新?
我试图更正您的代码,使用这个。
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');
}
我假设您使用的是Laravel 5。我认为问题在于这一行:
$article->update($request->all());
update方法需要一个列和值对的数组。你上面的代码是给它原始的(未修改的)请求,其中不包括你的新照片位置。
使用Laravel,您实际上可以获得article对象,直接在对象中设置列,然后保存更改。
尝试将方法的最后几行更改为:
$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文档以获得更多解释。