无法在laravel 5.4中编辑文档详细信息

时间:2019-01-02 21:25:53

标签: mysql laravel

在我的文档存档系统中,有一个编辑视图,该视图应允许用户编辑有关他们上载的文件的某些数据,甚至在需要时可以重新上传文件。出于某种原因,编辑视图无法打开,因为它说变量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 个答案:

答案 0 :(得分:0)

像$ doc这样的名称未定义,您应该确保在edit.blade.php文件上使用$ doc变量,该变量在处理显示的控件上是特定的。 如果您仍需要帮助,则应该发布该控制器。


更新:看一下文档:

https://laravel.com/docs/5.4/views#passing-data-to-views

解决方案:

return view('documents.edit', ['doc' => $doc,'categories' => $cate]);