how to add new action

Posted In Voyager
  • mostafa-khattab 2 years ago
    <p>hi 
    I love this admin panel but I can't figure out how to add new controller action 
    EX : add activate action to pages module ... I tried to use my own controller but for this, i have to rebuild the entire module from scratch again ... can I override or extend the main controller 
    ============
    last issue ... how can i insert the (auth()-&gt;user()-&gt;id) automaticlly in the author_id field when inserting data </p>
  • devdojo 2 years ago
    Hey There :)
    
    What kind of action are you trying to add? Are you trying to add some new functionality in a view?
    
    Additionally, if you want the current logged in Author ID to be saved you can add this method to your `App\Post.php` model, or whichever model you want to assign an author ID when it has been saved:
    
    ```
    public function save(array $options = [])
    {
        // If this is only a new create
        if (! isset($this->id)) {
            // Add the user id as the Author of the post
            $this->author_id = Auth::user()->id;
            parent::save();
        } else {
            parent::save();
        }
    }
    ```
    
    Add that to the model and the logged in user id will be stored in `author_id` field, or you may want to change it to the field that you want to add the User or Author ID.
    
    Hope that helps. Thanks.

    Hey There :)

    What kind of action are you trying to add? Are you trying to add some new functionality in a view?

    Additionally, if you want the current logged in Author ID to be saved you can add this method to your App\Post.php model, or whichever model you want to assign an author ID when it has been saved:

    public function save(array $options = [])
    {
        // If this is only a new create
        if (! isset($this->id)) {
            // Add the user id as the Author of the post
            $this->author_id = Auth::user()->id;
            parent::save();
        } else {
            parent::save();
        }
    }
    

    Add that to the model and the logged in user id will be stored in author_id field, or you may want to change it to the field that you want to add the User or Author ID.

    Hope that helps. Thanks.

  • almokhtar-bekkour 2 years ago
    hhhhhhhhhhhhhhhhh

    hhhhhhhhhhhhhhhhh

  • chist3r 2 years ago
    Why having `parent::save()` twice while you can have it once?
    
    ```
    public function save(array $options = [])
    {
        // If this is only a new create
        if (! isset($this-&gt;id)) {
            // Add the user id as the Author of the post
            $this-&gt;author_id = Auth::user()-&gt;id;
        }
            
        parent::save();
    }
    ```

    Why having parent::save() twice while you can have it once?

    public function save(array $options = [])
    {
        // If this is only a new create
        if (! isset($this-&gt;id)) {
            // Add the user id as the Author of the post
            $this-&gt;author_id = Auth::user()-&gt;id;
        }
            
        parent::save();
    }
    

Please login or signup to leave a response.

Login

or Sign Up

Sign Up

or Login
×
Want to create your own SAAS?

I can teach you to create your own Software as a Service. Click here to learn more!

Visit SAAS Adventure!