Paul
Paul

Reputation: 11

Deleting data from database using Laravel 8

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'
    ];
}

Upvotes: 0

Views: 1325

Answers (2)

Paul
Paul

Reputation: 11

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 ! :)

Upvotes: 1

Asad Ashfaq
Asad Ashfaq

Reputation: 74

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']);

Upvotes: 1

Related Questions