0

I'm stuck with an issue relating to deleting data from my database

The DELETE method is not supported for this route. Supported methods: GET, HEAD. -

My controller for deleting -

public function destroy($id) {

        $project = Projects::findOrFail($id);
        $project->delete();
        return redirect('/')->with('msg', 'Your project is now done');
   }

My form for deleting the data -

form action="/projects/{{ $project->id }}" method="POST">

            @csrf
            @method('DELETE')
            <button>Finish your project</button>
        </form>

My route -

Route::delete('/projects/{id}', [ProjectController::class, 'destroy']);

Migration -

public function up() {

        Schema::create('projects', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('type');
            $table->string('assignment');
            $table->string('name');
            $table->json('extra');
        });
    }

And lastly my models -

class Projects extends Model {

    use HasFactory;
    protected $casts = [
        'extra' => 'array'
    ];
}
4
  • Hi, have you tested by using POST method ? Commented Aug 28, 2021 at 19:09
  • What's that @method('DELETE') in your form code? Commented Aug 28, 2021 at 19:10
  • the @method('DELETE') is meant to sutitude the normal method="POST" in the form Commented Aug 28, 2021 at 19:14
  • @AmedeAngelAulerien Isn't the POST method meant to POST data into the database + I fake POST in the form and changing the POST into a @method('DELETE') ? Commented Aug 28, 2021 at 19:15

2 Answers 2

1

try this

Controller

public function destroy(Request $request) {

  $project = Projects::findOrFail($request->id);
  $project->delete();
  return redirect('/')->with('msg', 'Your project is now done');

}

View

<form action="/projects/delete" method="POST">  
  @csrf
  <input type="hidden" name="id" value="{{ $project->id }}"/> 
  <button>Finish your project</button>
</form>

Route

Route::post('/projects/delete', [ProjectController::class, 'destroy']);
Sign up to request clarification or add additional context in comments.

11 Comments

This gave me the same error message - The POST method is not supported for this route. Supported methods: GET, HEAD.
just change this <button type="submit">Finish your project</button>
also can you please share error message with me
Sorry for the delayed response - gyazo.com/c914ce46edefd7bebf6903d3eefa5fbc here's a screenshot of the error message
Route::post('/projects/delete', [ProjectController::class, 'drop]); public function drop(Request $request) please change the name destroy into drop
|
1

I used php artisan cache:clear at some point when debugging, which enables and clears cache. So the changes in my web.php routes file were not being applied, as they were cached.

Ty for the help guys ! :)

1 Comment

To sum it up, I ran "php artisan route:clear" and it fixed it up nicely

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.