Gör en FormRequest
objekt genom att utfärda följande kommando:
php artisan make:request YourFormRequest
Nu, i din reglermetod:
/**
* 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...
];
}
Uppdatera nu din kontroller:
/**
* 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.
}
}
UPPDATERING 1:
I din YourFormRequest
fil, ersätt auktoriseringsmetoden:
/**
* Authorize the request.
*
* @return bool
*/
public function authorize()
{
return true; // replace false with true.
}
Hoppas detta hjälper dig. Skål.