在我的文档存档系统中,有一个编辑视图,允许用户编辑他们上传的文件的某些数据,甚至在需要时重新上传一个文件。由于某些原因,edit视图没有打开,因为它表示表单中的变量am未定义。
我试过改变表格的细节,但没有任何效果。
这是edit.blade.php
<h3 class="box-title"><i class="fa fa-edit"></i> Edit Document</h3>
</div><!-- /.box-header -->
<div class="box-body">
{!! Form::open(['action' => ['DocumentsController@update', $doc->id],
'method' => 'PUT', 'enctype' => 'multipart/form-data']) !!}
{{ csrf_field() }}
<div class="form-group">
<label for="student_id">Student ID</label>
<select class="form-control" name="student_id">
@foreach($students as $student)
<option value="{{ $student->$id }}">{{ $student->$id }}
</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="category_id">Category</label>
<select class="form-control" name="category_id">
@foreach($categories as $category)
<option value="{{ $category->$id }}">{{ $category->name }}
</option>
@endforeach
</select>
</div>
This is the update funtion in the documents controller
public function update(Request $request, $id)
{
$this->validate($request, [
'file' => 'nullable|max:1999|unique:documents',
'category_id' => 'nullable|integer',
]);
$doc = Document::findOrFail($id);
$doc->category_id = $request->input('category_id');
//$doc->file = $path;
$doc->mimetype = Storage::mimeType($path);
$size = Storage::size($path);
if ($size >= 1000000) {
$doc->filesize = round($size/1000000) . 'MB';
}elseif ($size >= 1000) {
$doc->filesize = round($size/1000) . 'KB';
}else {
$doc->filesize = $size;
}
// determine whether it expires
if ($request->input('isExpire') == true) {
$doc->isExpire = false;
$doc->expires_at = null;
}else {
$doc->isExpire = true;
$doc->expires_at = $request->input('expires_at');
}
if($request->hasFile('file'))
{
// filename with extension
$fileNameWithExt = $request->file('file')
>getClientOriginalName();
// filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// extension
$extension = $request->file('file')-
>getClientOriginalExtension();
// filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
// upload file
$path = $request->file('file')->storeAs('public/uploaded
files/'.$user_id, $fileNameToStore);
Storage::delete('public/uploaded files/'. $doc->file);
$doc->file = $path;
}
$doc->save();
return redirect('documents')->with('flash_message','Successfully
Updated!');
Edit function in Controller
public function edit($id)
{
$doc = Document::findOrFail($id);
$categories = Category::all();
$cate = array();
foreach($categories as $category)
{
$cate[$category->id] = $category->name;
}
return view('documents.edit')->withDocuments($doc)->withCategories($cate);
//compact('doc','categories'));
}
this is the error
Undefined variable: doc (View:
C:\xampp\htdocs\unzadms\resources\views\documents\edit.blade.php)
I expect to be able edit the necessary file details.
1条答案
按热度按时间mwg9r5ms1#
看起来像$doc是未定义的,正如它所说的,您应该确保在edit.blade.php文件中使用$doc变量,特别是在处理显示的控制器上。如果你还需要帮助的话,你应该发布那个控制器。
更新:查看文档:
https://laravel.com/docs/5.4/views#passing-数据到视图
解决方案: