I have developed a webapp with laravel. Unfortunately I get a 404 after saving the form. But the logging shows me that the task was successfully updated.
Some stackoverflow postings cant helped me.
So in the log I have: [2025-07-01 11:31:57] local.INFO: Updating task with ID: 391
After saving, I am redirected to the URL /tasks/{id}. However, if I want to edit a task beforehand, I am redirected to the URL tasks/{id}/edit.
I have changed the routes and already checked everything (Put) etc. Unfortunately I have no more ideas. Here is my update method:
public function update(Request $request, Task $task) {
\Log::info('Updating task with ID: ' . $task->id);
$validated = $request->validate([
'title' => 'required|string|max:255',
'user_id' => 'nullable|exists:users,id',
'customer_id' => 'required|exists:customers,id',
'start_time' => 'nullable|date',
'end_time' => 'nullable|date|after_or_equal:start_time',
'status' => 'required|in:open,done,not_done,billed',
'comment' => 'nullable|string',
'categories' => 'nullable|array',
'categories.*' => 'exists:task_categories,id',
'contact_id' => 'nullable|exists:contacts,id',
]);
$user = User::findOrFail($validated['user_id']);
$customer = Customer::findOrFail($validated['customer_id']);
$task->contact_id = $request->input('contact_id');
$pdfPath = null;
$secondPdfPath = null;
// ⬇️ PDFs nur generieren, wenn aus Modal gespeichert wird
if ($request->input('request_source') === 'form') {
if ($request->filled('service_report_id')) {
$first_service = FirstServiceRecords::findOrFail($task->service_report_id);
$first_service_material_consumption = DB::table('service_materials')
->where('first_service_record_id', $task->service_report_id)->get();
$first_service_travel_details = DB::table('first_service_travel_details')
->where('first_service_record_id', $task->service_report_id)->get();
$pdf = Pdf::loadView('tasks.pdf', [
'modal_title' => $request->modal_title,
'modal_description' => $request->modal_description,
'user' => $user->name,
'customer' => $customer,
'service' => $first_service,
'service_travel_details' => $first_service_travel_details,
'service_material_consumption' => $first_service_material_consumption,
]);
$fileName = 'task_' . $task->id . '_' . time() . '.pdf';
$pdfPath = 'pdfs/' . $fileName;
$pdf->save(public_path($pdfPath));
}
if ($request->filled('second_service_report_id')) {
$service = ServiceRecord::findOrFail($request->second_service_report_id);
$service_material_consumption = DB::table('task_reports_material_consumption')
->where('service_record_id', $request->second_service_report_id)->get();
$service_travel_details = DB::table('service_travel_details')
->where('service_record_id', $request->second_service_report_id)->get();
$pdf2 = Pdf::loadView('tasks.pdf', [
'modal_title' => $request->modal_title,
'modal_description' => $request->modal_description,
'user' => $user->name,
'customer' => $customer,
'service' => $service,
'service_travel_details' => $service_travel_details,
'service_material_consumption' => $service_material_consumption,
]);
$secondFileName = 'second_task_' . $task->id . '_' . time() . '.pdf';
$secondPdfPath = 'second_pdfs/' . $secondFileName;
$pdf2->save(public_path($secondPdfPath));
}
}
// 🛠️ Task aktualisieren
$task->fill([
'title' => $validated['title'],
'user_id' => $validated['user_id'],
'customer_id' => $validated['customer_id'],
'status' => $validated['status'],
'start_time' => $validated['start_time'],
'end_time' => $validated['end_time'],
'comment' => $validated['comment'],
'modal_title' => $request->modal_title ?? '',
'modal_description' => $request->modal_description ?? '',
]);
if ($pdfPath) {
$task->pdf_path = $pdfPath;
}
if ($secondPdfPath) {
$task->second_pdf_path = $secondPdfPath;
}
if ($request->filled('service_report_id')) {
$task->service_report_id = $request->service_report_id;
}
if ($request->filled('second_service_report_id')) {
$task->second_service_report_id = $request->second_service_report_id;
}
$task->save();
// 🧹 Altes Auftrag-PDF ersetzen, nur wenn kein Urlaub/Krank
$isVacationOrSick = false;
if ($request->has('categories')) {
$urlaubId = TaskCategory::where('name', 'Urlaub')->first()?->id;
$krankId = TaskCategory::where('name', 'Krank')->first()?->id;
$selected = $request->input('categories', []);
$isVacationOrSick = in_array($urlaubId, $selected) || in_array($krankId, $selected);
}
if (!$isVacationOrSick) {
if ($task->pdf_path && file_exists(public_path($task->pdf_path))) {
unlink(public_path($task->pdf_path));
}
$taskWithRelations = Task::with(['customer', 'contact', 'user'])->find($task->id);
$auftragPdf = Pdf::loadView('tasks.pdf_auftrag', ['task' => $taskWithRelations]);
$auftragName = 'auftrag_' . $task->id . '_' . time() . '.pdf';
$auftragPath = 'pdfs/' . $auftragName;
$auftragPdf->save(public_path($auftragPath));
$task->pdf_path = $auftragPath;
$task->save();
}
// Kategorien synchronisieren
if (!empty($validated['categories'])) {
$task->categories()->sync($validated['categories']);
}
if ($request->ajax()) {
return response()->json(['message' => 'Aufgabe erfolgreich gespeichert.']);
}
return redirect()->route('tasks.edit', ['id' => $task->id])->with('success', 'Aufgabe erfolgreich gespeichert.');
}
My web.php:
Route::get('/tasks', [TaskController::class, 'index'])->name('tasks.index');
Route::get('/tasks/create', [TaskController::class, 'create'])->name('tasks.create');
Route::post('/tasks', [TaskController::class, 'store'])->name('tasks.store');
Route::get('/tasks/{id}', [TaskController::class, 'show'])->name('tasks.show');
Route::get('/tasks/{id}/edit', [TaskController::class, 'edit'])->name('tasks.edit');
Route::put('/tasks/{task}', [TaskController::class, 'update'])->name('tasks.update');
Route::delete('/tasks/{id}', [TaskController::class, 'destroy'])->name('tasks.destroy');
And my edit.blade.php
<form action="{{ route('tasks.update', $task->id) }}" method="POST">
@csrf
@method('PUT')
SOEM FIELDS
</form>
UPDATE
I have a edit function: ublic function edit($id) {
$task = Task::findOrFail($id);
$users = User::where('role', 'employee')->get();
$customers = Customer::all();
$categories = TaskCategory::all();
$diagnoses = Diagnosis::all();
$selectedDiagnosisId = $task->diagnosis_id;
$task_service_forms = DB::table('table_task_service_files')
->where('task_id', $id)
->where('status', 1)
->get();
$task_second_service_forms = DB::table('table_task_second_service_files')
->where('task_id', $id)
->where('status', 1)
->get();
// Prüfen, ob es eine AJAX-Anfrage ist (für Modal)
if (request()->ajax()) {
return view('tasks.partials.EditTaskModal', compact(
'task',
'users',
'customers',
'categories',
'diagnoses',
'selectedDiagnosisId',
'task_service_forms',
'task_second_service_forms'
));
}
// Normale Seite aufrufen
return view('tasks.edit', compact(
'task',
'users',
'customers',
'categories',
'diagnoses',
'selectedDiagnosisId',
'task_service_forms',
'task_second_service_forms'
));
}
SOLUTION FOUND:
I found the problem and would like to share it. It is possible to save a task without assigning it to a user
In the update function i make this:
$user = User::findOrFail($validated['user_id']);
$customer = Customer::findOrFail($validated['customer_id']);
I found the problem and would like to share it.
It is possible to save a task without assigning it to a user
In the update function i make this:
$user = User::findOrFail($validated['user_id']);
$customer = Customer::findOrFail($validated['customer_id']);