skip to Main Content

i can’t update table data in laravel 10 with controller resource

my code:

// controller
    public function update(Request $request, string $id):RedirectResponse
    {
        //
        $input = $request->validate([
            'id'=>'required',
            'name'=>'required',
            'description'=>'required',
        ]);

        $accounting_group = AccountingGroup::findOrFail($id);
        $accounting_group->update($input);

        return redirect()->route('accounting_group.index')->with(['success' => 'Data telah disimpan']);
    }
<!-- route -->
Route::resource('accounting_group', AccountingGroupController::class)->except(['show']);
<!-- model -->
class AccountingGroup extends Model
{
    use HasFactory;
    protected $table = 'accounting_groups';
    protected $fillable = [
        'id',
        'name',
        'description',
    ];

    protected $hidden = [];

    public function transactionaccount(): HasMany
    {
        return $this->hasMany(TransactionAccount::class);
    }
}
<form method="post" class="mx-2 p-4" action="{{ route('accounting_group.update', $accounting_group->id) }}">
               @csrf
               @method('put')
          <div class="form-group">
               <label for="id">ID Akun</label>
               <input type="text" class="form-control" id="id" placeholder="ID accounting..." value="{{ old('id', $accounting_group->id) }}">
          </div>
....
          <div class="d-flex justify-content-end">
               <button type="submit" class="btn btn-primary">Simpan</button>
          </div>
          </form>

i didn’t get eror display in laravel, i got refresh page where i clicked sumbit button and in mysql the data get no update

2

Answers


  1. To allow browser send input, you need to add you name attribute in your input for.

    So you just need to add name="id" to your:

    <input type="text" class="form-control" id="id" placeholder="ID accounting..." value="{{ old('id', $accounting_group->id) }}">
    

    so your code should be:

    <input type="text" class="form-control" id="id" name="id" placeholder="ID accounting..." value="{{ old('id', $accounting_group->id) }}">
    

    and don’t forgot to add name attribute to other fields like name="name" and name="description"

    Login or Signup to reply.
  2. When you are using Route::resource and call the show or update method then you will get the requested ID in the record in the controller function it is called property promotion

    Here I have enhanced your code

    public function update(Request $request, AccountingGroup $accounting_group):RedirectResponse
    {
        $input = $request->validate([
            'name'=>'required',
            'description'=>'required',
        ]);
    
        $accounting_group->update($input->validated());
    
        return redirect()->route('accounting_group.index')->with(['success' => 'Data telah disimpan']);
    }
    

    I have added the AccountingGroup $accounting_group in the
    argument. If this id record is not found then call the
    ModelNotFoundException

    And I have added the $input->validated() this method is only for
    returning the validated data, It’s used for security purposes.

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