dougaoshang0237 2015-12-22 06:16
浏览 85
已采纳

导致这个FatalErrorException的原因是什么?

I'm new in Laravel 5.1 Can you help me to solving this error because I've been working on this for whole day and I cannot solve it.

FatalErrorException in FileEntryController.php line 48: Call to undefined method App\Http\Requests\UploadFiles::save()

Here's my controller :

public function index()
{
    $entries = Fileentry::where('user_id',Auth::user()->id)->get();
    return view('fileentries.index', compact('entries'));
}


public function store(UploadFiles $filename)
{
      if($filename->file('filefield')) {
      $file = $filename->file('filefield');

      $entry = new UploadFiles();

      $extension = $file->getClientOriginalExtension();
      $entry->filename = $file->getClientOriginalName();

      $entry->mime = $file->getClientMimeType();
      $entry->original_filename = $file->getClientOriginalName();
      $entry->description = Request::input('description');
      $entry->user_id = Auth::user()->id;

      $entry->save();


      $file->move(Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file)));

      return redirect('upload');

       }
}

And here is my Request/UploadFiles.php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class UploadFiles extends Request
{
/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'filename' => 'mimes:pdf,doc,jpeg,png,docx',
        'description' => 'required',
    ];
}
  • 写回答

1条回答 默认 最新

  • duanmu5641 2015-12-22 09:50
    关注

    Your code will not work as expected, and some parts of it should be removed. First in your store() method, you have already imported UploadedFiles instance. From your code, I see UploadedFiles is a Request class, so this is fine.

    However, you should not make another UploadedFiles instance, within the store method, as this does not make sense.

    You should however make a new Model called (File) for instance, and you code should be something like below.

    public function store(UploadFiles $filename) { if($filename->file('filefield')) { $file = $filename->file('filefield');

      $entry = new Files();
    
      $extension = $file->getClientOriginalExtension();
      $entry->filename = $file->getClientOriginalName();
    
      $entry->mime = $file->getClientMimeType();
      $entry->original_filename = $file->getClientOriginalName();
      $entry->description = Request::input('description');
      $entry->user_id = Auth::user()->id;
    
      $entry->save();
    
    
      $file->move(Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file)));
    
      return redirect('upload');
    
       }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集