Очень странно, метод store у контроллера работает, но update нет.
Вот контроллер.
public function store(StoreEmployeeRequest $request)
{
if ($request->file()) {
$upload_path = public_path('upload/employees');
$generated_new_name = time() . '.' . $request->file('photo')->getClientOriginalExtension();
$request->file('photo')->move($upload_path, $generated_new_name);
$db_path = '/upload/employees/' . $generated_new_name;
$data = $request->validated();
$data['photo'] = $db_path;
$employee = Employee::create($data);
return new EmployeeResource($employee);
}
}
public function show(Employee $employee)
{
return new EmployeeResource($employee);
}
public function update(UpdateEmployeeRequest $request, Employee $employee)
{
$employee->update($request->validated());
return new EmployeeResource($employee);
}
И метод submit для страницы create.
Добрый день.
Очень странно, метод store у контроллера работает, но update нет.
Вот контроллер.
public function store(StoreEmployeeRequest $request)
{
if ($request->file()) {
$upload_path = public_path('upload/employees');
$generated_new_name = time() . '.' . $request->file('photo')->getClientOriginalExtension();
$request->file('photo')->move($upload_path, $generated_new_name);
$db_path = '/upload/employees/' . $generated_new_name;
$data = $request->validated();
$data['photo'] = $db_path;
$employee = Employee::create($data);
return new EmployeeResource($employee);
}
}
public function show(Employee $employee)
{
return new EmployeeResource($employee);
}
public function update(UpdateEmployeeRequest $request, Employee $employee)
{
$employee->update($request->validated());
return new EmployeeResource($employee);
}
И метод submit для страницы create.
onSubmit() {
let form = new FormData();
for (let key in this.form) {
form.append(key, this.form[key]);
}
axios
.post("/api/auth/employee", form)
.then((res) => {
console.log(res.data, "res");
// this.$router.push({ name: "admin.employee" });
})
.catch((error) => {
this.errors = error.response.data.errors;
// this.form_submitting = false;
});
},
И тот же submit для страницы edit.
onSubmit() {
const config = {
headers: {
"content-type": "multipart/form-data",
},
};
let form = new FormData();
for (let key in this.form) {
form.append(key, this.form[key]);
}
axios
.put("/api/auth/employee/" + this.$route.params.id, form, config)
.then((res) => {
console.log(res.data, "res");
// this.$router.push({ name: "admin.employee" });
})
.catch((error) => {
this.errors = error.response.data.errors;
// this.form_submitting = false;
});
},
},
Тут еще добавляются заголовки, но что с ними, что без них, но request в методе update контроллера пустой.
Поменял метод update, и request пустой.
public function update(Request $request, Employee $employee)
{
return response()->json([
'result' => $request->all()
]);
$employee->update($request->validated());
return new EmployeeResource($employee);
}
Почему?