12

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..

SuperStar518
  • 2,814
  • 2
  • 20
  • 35
Mette
  • 207
  • 1
  • 4
  • 18

11 Answers11

29

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.

Akeel ahamed
  • 877
  • 11
  • 11
  • 1
    @DoreenChemweno check this link https://stackoverflow.com/questions/18592836/eloquent-collections-each-vs-foreach – Akeel ahamed Apr 01 '20 at 04:30
18

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();
}
Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
  • but then he does not have the post, just a query builder. – Alex Harris May 10 '17 at 18:09
  • If you want to delete first document, use first() instead of get(). – Mihir Bhende May 10 '17 at 18:12
  • Use dd($post) to debug. It its null means the document with the mentioned id and user_id was not found. Then you need to check you parameters. For now pass static parameters for id and user_id and check first. If it works means the delete function is working, Then you can check about parameters passed – Mihir Bhende May 10 '17 at 18:21
3

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

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

davefrassoni
  • 306
  • 2
  • 5
2

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);
    }
}
Agu Dondo
  • 12,638
  • 7
  • 57
  • 68
0

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.

Mill3r
  • 544
  • 1
  • 10
  • 31
0

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');
}
Priyanka khullar
  • 509
  • 1
  • 5
  • 25
0

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)
Rahul
  • 18,271
  • 7
  • 41
  • 60
0

This is how I am able to delete one (or multiple) associated record(s) in a table:

RFIResponseFile::where('rfi_response_id',$response_id)->delete();
hackernewbie
  • 1,606
  • 20
  • 13
0
Post::where('id', $id)->where('user_id',$user_id)->delete();

Or

$post= Post::where('id', $id)->where('user_id',$user_id);
if($post->get()->count()){
    $post->delete();
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 21 '22 at 06:33
-1

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
        ]);
    }
Brijesh Dubey
  • 118
  • 2
  • 12
-1

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