0

I want to delete a record from my table named post. I am sending a param named tag in my view to delete a certain record against this tag. So here is my route

   Route::get('/delete' , array('as' =>'delete' , 'uses' => 'Postcontroller@deletepost'));

by this route i am delete my post against it's 'tag' field. my table has two column. one is tag and other is content My delete fucntion in PostController is

   public function deletepost($tag){

   $post = post::find($tag); //this is line 28 in my fuction
   $post->delete();
   echo ('record is deleted') ;
   }

I am send tag from my view but it is giving the following error

  ErrorException in Postcontroller.php line 28:
  Missing argument 1 for
  App\Http\Controllers\Postcontroller::deletepost()

3 Answers 3

1

Your action should look like this:

use Illuminate\Http\Request;

public function deletepost(Request $request) // add Request to get the post data
{
    $tagId = $request->input('id'); // here you define $tagId by the post data you send
    $post = post::find($tagId);
    if ($post) {
        $post->delete();
        echo ('record is deleted!');
    } else {
        echo 'record not found!');
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

public function deletepost(Request $request) { $tagId = $request->input('tag'); $post = post::find($tagId); $post->delete($tagId ); echo ('record is deleted') ; } by changing this followinf error came Call to a member function delete() on null
And change $tagId = $request->input('id');, the id to the name of the post id identifier sent by the post request.
i think in 5.3 we have to use get method instead of input. but your logic worked. Thanks and if we want to delete any record on the custom bases , other than primary key we have to specify our condition.
Actually I was playing around with Laravel since 5.4, but I'm glad you've found the solution.
1

You have to pass the parameter for an example if you pass it like tag_id then you have to capture it inside the controller function using Request.

public function deletepost(Request $request){

   $post = post::find($request::get('tag_id')); 
   $post->delete();
   echo ('record is deleted');
}

1 Comment

Your Welcome :D Qadeer_Sipra
0

You are not telling the route to expect that parameter. You should try it this way in your routes file:

Route::get('/delete/{tag}' , array('as' =>'delete' , 'uses' => 'Postcontroller@deletepost'));

1 Comment

NotFoundHttpException in RouteCollection.php line 161: now browser is show this error

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.