Home  >  Q&A  >  body text

Laravel deletes a record. The record can only be deleted by its owner. How should the `destroy()` method be added?

For example, if an article is to be deleted, only the creator of this article can delete it. How should the following destroy() method be added?


public function destroy($id)
    {
        $user = \Auth::user();
        //...
        Article::destroy($id);
        //...
    }
阿神阿神2713 days ago393

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 16:51:51

    Pseudo code:

    if(Article::findByid($id)->getAuthorId()==$user->id){
        Article::destroy()
    }else{
        throw new Exception("没有删除权限");
    }

    The relatively correct approach is,

    1. Don’t let him have the opportunity to press the delete button (hidden on the interface)
    2. Judge before deleting

    reply
    0
  • Cancelreply