Направете FormRequest
обект чрез подаване на следната команда:
php artisan make:request YourFormRequest
Сега във вашия метод с правила:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'filename' => 'mimes:pdf,doc,jpeg,png,docx',
// and other validation rules...
];
}
Сега актуализирайте своя контролер:
/**
* Store the form values.
* Don't forget to import the YourFormRequest class
*
* @param \App\Http\Requests\YourFormRequest $request
* @return \Illuminate\Http\Redirect|string
*/
public function store(YourFormRequest $request)
{
if($request->file('filename')) {
$file = $request->file('filename');
$fileName = $file->getClientOriginalName();
$fileExt = $file->getClientOriginalExtension();
$fileMime = $file->getClientMimeType();
// and rest of the file details
// Move the file now
$updatedFileName = $fileName.'.'.$fileExt;
$file->move('path/to/destination/folder', $updatedFileName);
// or using the Storage class, it is the same
// as what you have written.
}
}
АКТУАЛИЗАЦИЯ 1:
Във вашия YourFormRequest
файл, заменете метода за оторизиране:
/**
* Authorize the request.
*
* @return bool
*/
public function authorize()
{
return true; // replace false with true.
}
Надявам се това да ви помогне. Наздраве.