我是Laravel 5.1的新手你能否帮助我解决这个错误,因为我一整天都在努力解决这个问题。我无法解决它。
FileEntryController.php第48行中的FatalErrorException:调用 undefined方法App \ Http \ Requests \ UploadFiles :: save()
这是我的控制器:
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');
}
}
这是我的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',
];
}
答案 0 :(得分:1)
您的代码将无法按预期工作,并且应删除其中的某些部分。
首先在store()
方法中,您已导入UploadedFiles
个实例。从您的代码中,我看到UploadedFiles
是一个Request类,所以这很好。
但是,您不应在UploadedFiles
方法中创建另一个store
实例,因为这没有意义。
然而,您应该创建一个名为(File
)的新模型,并且您的代码应该类似于下面的内容。
公共功能商店(UploadFiles $ filename) { if($ filename-> file(' filefield')){ $ file = $ filename->文件(' 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');
}
}