Laravel - 方法删除不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43899678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:56:58  来源:igfitidea点击:

Laravel - Method delete does not exist

phplaravel

提问by Mette

I'm getting the error:

我收到错误:

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

Macroable.php 第 74 行中的 BadMethodCallException:方法删除不存在。

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

我尝试更改method="post"delete:错误消失但没有删除任何内容..

回答by Mihir Bhende

Remove get() and it will work

删除 get() 它将起作用

$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 :

但是,您始终需要检查 $post 是否作为查询文档或它的 null 被找到,因此添加:

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

回答by Artyom_akeel

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,

->each()像这样在删除之前添加,

$post->each->delete();

It work's for me.

这个对我有用。

回答by davefrassoni

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

使用->first()而不是->get()

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

你不能用delete()删除整个集合

回答by Agu Dondo

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

更改getfirst,然后检查该帖子是否属于该用户。

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

回答by Mill3r

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.

旗帜。

回答by Priyanka khullar

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

回答by Rahul

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

由于 post id 是posts表的主键,您可以直接从表中删除,
不需要user_id

To fetch user_id from Auth facade you should use,

要从您应该使用的 Auth 外观中获取 user_id,

$user_id = Auth::id();

Only by passing id should work,

只有通过传递 id 才能工作,

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 destroymethod. In addition to a single primary key as its argument, the destroymethod will accept multiple primary keys, an array of primary keys, or a collectionof primary keys:

但是,如果您知道模型的主键,则可以通过调用该destroy方法删除模型而不检索它。除了单个主键作为其参数外,该destroy方法还将接受多个主键、主键数组或主键集合

Post::destroy($id)

回答by Brijesh Dubey

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
        ]);
    }

回答by winnerawan

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