Kaca Lebu
Kaca Lebu

Reputation: 19

my delete query is not working in laravel

I am trying delete database data in Laravel. but this is not working my way.

my view page is

{{url('/deleteReview/'.$Review->id)}}

my web is

Route::post('/deleteReview/{id}','adminController@deleteReview');

my controller delete function is

  public function deleteReview($id){
         $deleteReview = Review::find($id);

          $deleteReview->delete();

         return redirect('/manageReview');

    }

Upvotes: 0

Views: 237

Answers (2)

BlueC
BlueC

Reputation: 1581

Are you trying to delete the review by opening the page /deleteReview/<id> in your browser? If so, this would be a GET request, so change the route to a get route:

 Route::get('/deleteReview/{id}','adminController@deleteReview');

Please note as per the comments that a GET request should never change data server side. If data is changed using a GET request then there is a risk that spiders or browser prefetch will delete the data.

The correct way to do this in Laravel is using a POST request and use Form Method Spoofing to simulate a DELETE request. Your route entry would then look like this:

Route::delete('/deleteReview/{id}','adminController@deleteReview');

And your form would look like this:

<form action="/deleteReview/{{ $Review->id }}" method="POST">
    <input type="hidden" name="_method" value="DELETE">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

Upvotes: 2

Rohan Sable
Rohan Sable

Reputation: 25

At Controller you should first set Validation for ID that you have to Delete. Create your own customize request handler such as DeleteRequest. Once you get ID at Controller then used this code

 public function deleteReview(DeleteRequest $id){
             DB::table('reviews')->where('id', $id)->delete();
             return redirect('/manageReview'); 
    }

I hope it will work.

Upvotes: 0

Related Questions