我已经建立了一些我希望工作的东西。它应该上传文件并替换'附件'使用新文件的路径输入记录。这是方法:
public function update($id)
{
$article = Article::find($id);
$article->username = Session::get('username');
$article->body = Input::get('body');
$article->title = Input::get('title');
if(Input::hasFile('attachment'))
{
$file = Input::file('attachment');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(public_path() . '/documents/articles/', $name);
$article->attachment = $name;;
}
$article->save();//Commit this edit to the database
return Redirect::route('articles.index');
}
形式:
<div class="form-general">
{{ Form::model($article, array('route' => array('articles.update', $article->id, 'files' => true), 'method' => 'PUT')) }}
<div>
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, array('class' => 'form-control' )) }}
</div>
<br />
<div>
@if(! empty($article->attachment))
{{ Form::label('attachment', 'Attachment:') }} <br />
<div class="attachment-replace" style="display:none;">
{{ Form::file('attachment', null, array('class' => 'form-control' )) }}
</div>
<a class="current-attachment" href="/documents/articles/{{ $article->attachment }}">{{ $article->attachment }}</a> <a href="#" class="current-attachment"><span class="glyphicon glyphicon-remove-circle remove-attachment"></span></a>
@else
{{ Form::label('attachment', 'Attachment:') }}
{{ Form::file('attachment', null, array('class' => 'form-control' )) }}
@endif
</div>
<br />
<div>
{{ Form::label('body', 'Body:') }}
{{ Form::textarea('body', null, array('class' => 'form-control' )) }}
</div>
<br />
<div>{{ Form::submit('Edit Post', array('class' => 'btn btn-default')) }}</div>
{{ Form::close() }}
</div>
目前,没有任何反应。该方法被执行,其他字段更新正常,但附件没有。似乎文件输入&#34;附件&#34;从来没有设定过。如何正确更新文件路径并立即上传其替换文档?
答案 0 :(得分:3)
您的files
参数是错误数组的一部分。你有这一行,它打开你的表格:
{{ Form::model($article, array('route' => array('articles.update', $article->id, 'files' => true), 'method' => 'PUT')) }}
如果仔细观察,您会发现files => true
选项是route
数组的一部分,但它应该在它之外。像这样:
{{ Form::model($article, array('route' => array('articles.update', $article->id), 'files' => true, 'method' => 'PUT')) }}
正如他们所说,魔鬼在细节中;)