Home > database >  Updating database records from a form using Laravel
Updating database records from a form using Laravel

Time:01-08

I am new to Laravel and I am trying to update a grantor's details via a form, however the record in the database table is not registering any changes. I have tried a number of different solutions but unfortunately I've not been successful yet - I have a feeling the issue may involve the update method in the controller not being reached, as when I place a return/redirect statement in the update method this seems to be ignored. The edit method appears to work correctly, however the update method does not alter the database record. I will include snippets of my existing code for clarity. Any help with this problem would be greatly appreciated!

Grantor Controller File

public function update(GrantorRequest $request, $id)
{
    $grantor = Grantor::findOrFail($id);
    $request->validate([
        'grantor_name' => 'required'
    ]);
    $updateDetails = [
        'grantor_name' => $request->get('grantor_name')
    ];
    $grantor->update($updateDetails);
    return redirect()->route('grantors.index');
}

Note 'grantor_name' is an example of one field in the database row, there are multiple others which would also require updating.

Grantors.edit.blade File

<div >
      <form method="PUT" action="{{ route('grantors.update', $grantor->id) }}" > 
        
      @method('PATCH')
      @csrf
      

        <div >
            @if ($errors->any()) <span>{{ $errors }}</span> @endif
         </div>
         <div >

            <!-- item -->
            <div x-data=" {isOpen : false} " >
              <div @click="isOpen = !isOpen"
                
                :>
                <span >Grantor Details</span>
                <img  :
                    src="../../img/icon-arrow-down.png" alt="missing">
              </div>
              <div x-show.transition.duration.300ms.origin.bottom="isOpen" x-cloak @click.away="isOpen = false"
                >
                @if($is_editable)
                <div >
                <button type="button" ><img  src="../../img/edit-icon.svg" alt="edit"></button>
                <button type="button" ><img  src="../../img/save-icon.svg" alt="save"></button>
                </div>
                @endif
                <form >
                  <div >
                    <div >
                      <label  for="grantor_name"  for="inline-full-name" disabled>Grantor/ Company/ Organisation Name *
                      </label>
                    </div>
                    <div >
                      <input  name="grantor_name" value="{{ old('grantor_name') ?? $grantor->grantor_name }}" id="grantor_name" type="text" disabled >
                    </div>
                  </div>

Tenant Routes File

Route::get('/grantors/{id}/edit', [App\Http\Controllers\GrantorController::class, 'edit'])->name('grantors.edit');
Route::put('/grantors', [App\Http\Controllers\GrantorController::class, 'update'])->name('grantors.update');

Grantor Model

protected $fillable= [
    'grantor_name'
]);

Note that there are many other fields included in the $fillable which I have not included for purposes of length.

CodePudding user response:

Because you're not actually updating the model. You need to call update on your model:

    public function update(GrantorRequest $request, $id)
    {
        // Dont do `toArray` . You'll lose the Eloquent model. 
        $grantor = Grantor::findOrFail($id);
        $request->validate([
            'grantor_name' => 'required'
        ]);
        $updateDetails = [
            'grantor_name' => $request->get('grantor_name')
        ];
        // Notice the call below
        $grantor->update($updateDetails);

        return redirect()->route('grantors.index');
    }

The route definition should be changed as well:

// `id` param was missing
 Route::patch('/grantors/{id}', [App\Http\Controllers\GrantorController::class, 'update'])->name('grantors.update');

And html forms do not support patch or put method. You should do it like below:

 <form method="POST" action="{{ route('grantors.update', $grantor->id) }}" > 
        
      @method('PATCH')
...

CodePudding user response:

  1. Change your Grantors.edit.blade.php File:
<div >
      <form method="POST" action="{{ route('grantors.update', ['id' => $grantor->id]) }}" > 
        
      @method('PUT')
      @csrf
// ...

Notice the changes:

1a) method="PUT" to method="POST"

1b) @method('PATCH') to @method('PUT')

1c) {{ route('grantors.update', $grantor->id) }} to {{ route('grantors.update', ['id' => $grantor->id]) }}

``

  1. Change the Tenant Routes File
// ...
Route::put('/grantors/{id}', [App\Http\Controllers\GrantorController::class, 'update'])->name('grantors.update');
// ...

2a) Included the /{id} segment in the route.

  1. Make sure you have one <form> tag in your Grantors.edit.blade.php file.

From the looks of it, you have 2 <form> tags. One nested inside another.

<form method="PUT" action="{{ route('grantors.update', $grantor->id) }}" >

and

<form >

  1. Clear route cache if cached previously.

In your terminal, run the command below:

php artisan cache:clear

  •  Tags:  
  • Related