Controller:
public function updateTeachers(Request $request, User $teachers){
$validated = $request->validate([
"name" => ['required'],
"email" => ['required'],
]);
$teachers->update($validated);
return back()->with('information-message', 'Data was successfully updated');
}
View:
<form action="/teacher/{{ $dataTeacher->id }}" method="POST" class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-80 px-4 py-3">
@method('PUT')
@csrf
<div class="flex flex-wrap -mx-3 mb-6">
<div class="w-full px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
Name
</label>
<input type="text" name="name" value="{{ $dataTeacher->name }}" class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" required>
</div>
</div>
<div class="flex flex-wrap -mx-3 mb-6">
<div class="w-full px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-password">
Email
</label>
<input type="email" name="email" value="{{ $dataTeacher->email}}" class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" required>
</div>
</div>
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Push</button>
Route:
Route::put('/teacher/{teacher}', [UserController::class, 'updateTeachers']);
Is my update code wrong? It’s not working it shows only the information-message.
Any way to use the update in laravel?
2
Answers
There are actually many ways you can update it.
First, make sure your model has added "
name
" and "email
" to the fillable property.Step 2 :
Step 3:
Step 4:
One of these might help you. Try these methods.
Happy Coding.