skip to Main Content

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


  1. 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 :

    $teachers->update([
       'name' => 'John Doe',
       'email' => '[email protected]',
    ]);
    

    Step 3:

    $data = $teachers;
    $data->name = $request->name;
    $data->email= $request->email;
    $data->save();
    

    Step 4:

    use IlluminateSupportFacadesDB;
    
    DB::table('users')->where('id', $teachers->id)->update([
            'name' => 'John Doe',
           'email' => '[email protected]',
    ]);
    

    One of these might help you. Try these methods.

    Happy Coding.

    Login or Signup to reply.
  2. Route::put('/teacher/{user}', [UserController::class, 'updateTeachers']);
    

    https://laravel.com/docs/10.x/routing#implicit-binding

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search