Search code examples
phplaravel-5.3

Laravel 5 How to RESTRICT user not able access an Article if the Article STATUS is "Draft"


i want to restrict the user not able access the article if the article is not published, and is in draft mode. For example, an article id is 1, and is in draft mode, when user type url: www.example.com/1, the user still can access it. Is there a way that i can do it in middleware or use model route binding, so that it will check the Article status. If article is published, user can access, or else if article is draft, user cannot access it. I try google, but still can find the best way to solve this.


Solution

  • Assuming that you save the modes in the database, you can just check this, or in your route or in your controller or in your view.

    I would recommend you doing this in your controller, so you can do something like this;

    if($article->status == "draft"){
          return redirect()->to('your_url_here');
    }
    //other code here
    

    If you get what I mean?