Summer Sale! All accounts are 50% off this week.

redroseamit's avatar

update function is not working laravel 6 i am using resource controller

/* here is the controller */ public function update(Request $request, $id) {

    $categories = Categories::findOrFail($id);

    $this->validate($request, [
        'id' => 'required',
        'name' => 'required',
        'status' => 'required',
       
    ]);

    $input = $request->all();

    $categories->fill($input)->save();

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

/*here is the form */

          <label for="id">ID:</label>
          <input type="text" class="form-control" value="{{$categories->id}}" name="id"/>
      </div>
      <div class="form-group">
          <label for="name">Name :</label>
          <input type="text" class="form-control" value="{{$categories->name}}" name="name"/>
      </div>
      <div class="form-group">
          <label for="status">status</label>
          <input type="text" class="form-control" value="{{$categories->status}}"  name="status"  />
      </div>
     
      <button type="submit" class="btn btn-primary">UPDATE</button>
  </form>
0 likes
22 replies
redroseamit's avatar

form method="PUT" action= "{{ action('CategoriesController@update',$categories->id) }}

MichalOravec's avatar
Level 75

Why your model is Categories and not in the singular form like Category?

Why do you allow to update id?!

In routes

Route::resource('categories', 'CategoriesController');

In controller

public function update(Request $request, Category $category)
{
    // validation without id

    $category->update($request->all());

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

In view

<form action="{{ route('categories.update', ['category' => $category]) }}" method="POST">
     @csrf

     @method('PUT')

    <!-- inputs without id -->
</form>

Read this part of documentation: https://laravel.com/docs/7.x/controllers#resource-controllers

1 like
Thibaultvanc's avatar

Do you have any errors ? How do you see it dos not work ?

redroseamit's avatar

madam should i need to change categories model to category ....why ?? i want to update id ...means i created a table inside database which name is categories so there are three column inside category id,name,status...i want when user update something so it will change according to selected id ... example id 1 is selected for update so it will update data of id 1 like name and status...

Thibaultvanc's avatar

as @michaloravec say, you need to specify @method('PUT') in your form

Also I suggest you to place a temprary dd('here'); in your controller method to be sure you hit the method

redroseamit's avatar

sir i try that not working ...its showing Symfony\Component\Debug\Exception\FatalThrowableError syntax error, unexpected '$categories' (T_VARIABLE)

eggplantSword's avatar

@redroseamit If your table in the migrations is correct you don't need to send the id, because Laravel will take care of that, updating the id yourself could be dangerous as the wrong id could be sent instead. I also second @thibaultvanc you should add a dd('here'); to debug and make sure you're even getting into the controller update function.

Thibaultvanc's avatar

@redroseamit

  • did you place the dd('here'); to be sure you hit the good controller method ?
  • di you see 'here' at the screen while updating ?
redroseamit's avatar

no sir i am confused ...sir can u please guide me what steps should i need follow .. can you please provide code for like i have categories table in database there are three column id name and status ....i want to perform create,update, and delete but only create and delete is working ...i will be more obliged ..

redroseamit's avatar

madam i made all the changes but now update button is not working.....its not functioning mean neither refreshing page nor updating its stuck continue on update page....

MichalOravec's avatar

Without your actual code we can't know what are you doing wrong.

redroseamit's avatar
@extends('layouts.app')

@section('pageTitle', 'Edit Categories Details')

@section('content')

    <h1 class="display-6">Edit Categories</h1>

    <hr/>
    <!-- if validation in the controller fails, show the errors -->
    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

 

<form action="{{ route('categories.update', ['category' => $category]) }}" method="POST">
     @csrf

     @method('PUT')

    <!-- inputs without id -->
</form>




              <label for="id">ID:</label>
              <input type="text" class="form-control" value="{{$category->id}}" name="id"/>
          </div>
          <div class="form-group">
              <label for="name">Name :</label>
              <input type="text" class="form-control" value="{{$category->name}}" name="name"/>
          </div>
          <div class="form-group">
              <label for="status">status</label>
              <input type="text" class="form-control" value="{{$category->status}}"  name="status"  />
          </div>
         
          <button type="submit" class="btn btn-primary">UPDATE</button>
      </form>
@endsection

MichalOravec's avatar

Where is the form? Why is there still input where you change id?

Put your code correctly.

On the top and bottom put ```

MichalOravec's avatar

You have to think, when somebody give you an advice.

<form action="{{ route('categories.update', ['category' => $category]) }}" method="POST">
    @csrf

    @method('PUT')

    <div class="form-group">
        <label for="name">Name</label>

        <input type="text" class="form-control" value="{{ $category->name }}" name="name">
    </div>

    <div class="form-group">
        <label for="status">Status</label>

        <input type="text" class="form-control" value="{{ $category->status }}"  name="status">
    </div>

    <button type="submit" class="btn btn-primary">Update</button>
</form>

This was just where you should paste your inputs...

<!-- inputs without id -->
1 like
redroseamit's avatar

thank you so much madam , thanks a lot lot....i am obliged .... i was trying to fix this issue from 4 days and almost 4 days wasted behind it ...thank you again and thanks for your valuable efforts and kindness...

Please or to participate in this conversation.