0

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

    }

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Does a review with the given id exist? Try dumping $id before the find to check that the controller method is receiving what you think it is.
While you've identified the issue, the solution you propose is highly problematic - deleting stuff via a GET is a bad idea. Google crawling your site would delete things; so would a browser that decides to pre-fetch the link you just hovered over.
@ceejayoz absolutely, a get request should never change data on the server side. I'll update my answer to reflect this.
0

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.

Comments

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.