Home » Php » php – Laravel – Method delete does not exist

php – Laravel – Method delete does not exist

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m getting the error:

BadMethodCallException in Macroable.php line 74: Method delete does
not exist.

route:

Route::resource('posts', 'PostController');

my controller:

public function destroy($id)
{
    $user_id = Auth::user();
    $post= Post::where('id', $id)->where('user_id',$user_id)->get();
    $post->delete();

    return view('/home', [
        'posts' => $post
    ]);
}

view:

  <form action="{{ route('posts.destroy', '$post->id') }}" method="post">
  <input type="hidden" name="_method" value="DELETE" />
      {{ csrf_field() }}
       {{ method_field('DELETE') }}

   <input type="submit" class="btn btn-danger" value="delete" />
  </form>

I tried changing method="post" to delete: error is gone but nothing gets deleted..

How to&Answers:

Remove get() and it will work

$post= Post::where('id', $id)->where('user_id',$user_id);
$post->delete();

If you want to delete first document you can use : 

$post= Post::where('id', $id)->where('user_id',$user_id)->first();
    $post->delete();

However, you always need to check if $post is found as a query document or its null so addd :

if($post){
$post->delete();
}

Answer:

This is your code.

$user_id = Auth::user();
$post= Post::where('id', $id)->where('user_id',$user_id)->get();
$post->delete();

just add ->each() before delete like this,

$post->each->delete();

It work’s for me.

Answer:

Change get for first, and check if the post belongs to the user afterwards.

public function destroy($id)
{
    $post = Post::where('id', $id)->first();
    if($post && $post->user_id == \Auth::user()->id){
         $post->delete();
         return view('/home');
    }else{
        abort(404);
    }
}

Answer:

use ->first() instead of ->get()

you can’t delete an entire collection with delete()

Answer:

controller:

public function destroy($id)
    {
        $post = Post::find($id);
        $post->delete();
        //redirect to
        return redirect()->back();
    }

view:

{!! Form::open(['method' => 'DELETE','route' => ['posts.destroy', $post->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

Try this.

hope you created the controller with

–resource

flag.

Answer:

As post id is primary key of posts table you can directly remove from table,
No need of user_id

To fetch user_id from Auth facade you should use,

$user_id = Auth::id();

Only by passing id should work,

Post::find($id)->delete()

However, if you know the primary key of the model, you may delete the model without retrieving it by calling the destroy method. In addition to a single primary key as its argument, the destroy method will accept multiple primary keys, an array of primary keys, or a collection of primary keys:

Post::destroy($id)

Answer:

Please Try This:

public function destroy($id)
{
    $userId = Auth::user()->id;
    $post = Post::where([
                    'id' => $id,
                    'user_id' => $userId

            ])->delete();

    Session::flash('success', 'Post was successfully deleted!');
    return redirect()->route('posts.index');
}

Answer:

In your controller

Before change code

public function destroy($id)
{
    $user_id = Auth::user();
    $post= Post::where('id', $id)->where('user_id',$user_id)->get();
    $post->delete();

    return view('/home', [
        'posts' => $post
    ]);
}

After change code

    public function destroy($id)
    {
        $user_id = Auth::user();
        $post= Post::where(['id'=>$id,'user_id'=>$user_id])->get();
               Post::where(['id'=>$id,'user_id'=>$user_id])->delete();

        return view('/home', [
            'posts' => $post
        ]);
    }

Answer:

Just add this on top of view, i ‘ve error like you and now solved, sorry for bad english.

 {!! Form::model($post, ['route' => ['posts.destroy', $post->id], 'method' => 'DELETE']) !!}

and

{!! Form::close() !!} 

on bottom

for controller

$post = Post::find($id);
$post->delete();

Session::flash('success', 'Menu was successfully deleted!');
return redirect()->route('posts.index');