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',
];
}
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');
}
}