#laravel #frontend
#laravel #интерфейс
Вопрос:
Приветствую, я пытаюсь загрузить файл из интерфейса в серверный интерфейс Laravel API, но он всегда возвращает требуемую ошибку
Я записал данные в консоль, и я вижу, что поле существует, но Laravel его не видит
console.log()
{name: 'aaaaaaaaaa', gender: 'male', date_of_birth: '2021-01-01', identity: '22558741256932',
test: 'aaaaaaaaaa', …}
branch: "aaaaaaaaaaa"
date_of_birth: "2021-01-01"
gender: "male"
identity: "22558741256932"
lab_director: "aaaaaaaaaaaaaaaaaaaaa"
name: "aaaaaaaaaa"
qr_url: "aldMFEQOaOWT9xqRXrSTc3jMq0OwlQRQilk7LjuFVyr05AjYQ4MnC1q5FqOpUqRb6dK1a6MQmjZR8qGiqkBjeUnClKEJT0x5KOm0qexIUtbeowzjjsSoH2Yr637g1QxxXjESJPuf2yPszrlFP7wxn3"
reference_range: "Negative"
register_date: "2021-01-01"
report: File {name: 'test (2).pdf', lastModified: 1639022863879, lastModifiedDate: Thu Dec 09 2021 06:07:43 GMT 0200 (Eastern European Standard Time), webkitRelativePath: '', size: 2888211, …}
result: "Negative"
result_date: "2021-01-01"
test: "aaaaaaaaaa"
[[Prototype]]: Object
поле, отображающее ошибку, является полем «отчет»
Код Laravel
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|min:3|max:100',
'gender' => 'required|min:4|max:6',
'date_of_birth' => 'required',
'identity' => 'required|min:14|max:14',
'test' => 'required|min:3|max:100',
'result' => 'required|min:8|max:8',
'register_date' => 'required',
'result_date' => 'required',
'branch' => 'required|min:3|max:100',
'reference_range' => 'required|min:8|max:8',
'lab_director' => 'required|min:3|max:100',
'qr_url' => 'required|min:150|max:150',
'report' => 'required'
]);
if ($validator->fails()) {
$errors = $validator->errors();
return response()->json($errors);
}
$report = $request->file('report');
$ext = $report->getClientOriginalExtension();
$report_name = $request->qr_url . ".$ext";
$report->move(public_path('uploads/reports/'), $report_name);
$name = $request->name;
$gender = $request->gender;
$date_of_birth = $request->date_of_birth;
$identity = $request->identity;
$test = $request->test;
$result = $request->resu<
$register_date = $request->register_date;
$result_date = $request->result_date;
$branch = $request->branch;
$reference_range = $request->reference_range;
$lab_director = $request->lab_director;
$qr_url = $request->qr_url;
$report = $request->report;
$user = User::create(['name' => $name, 'gender' => $gender, 'date_of_birth' => $date_of_birth, 'identity' => $identity, 'test' => $test, 'result' => $result, 'register_date' => $register_date, 'result_date' => $result_date, 'branch' => $branch, 'reference_range' => $reference_range, 'lab_director' => $lab_director, 'qr_url' => $qr_url, 'report' => $report]);
$success = 'User Added Successfully';
return response()->json($success);
}
он всегда возвращает report field is required
Комментарии:
1. вам нужно отправить файл через
FormData()
объект developer.mozilla.org/en-US/docs/Web/API/FormData