How to save logged user_id using bread?

Solved Posted In Voyager
  • hermesalvesbr 1 week ago
    Hi,
    
    How can i get the user_id (logged user) on save? 
    
    I want to know wich user add in table. 
    
    Like this:
    https://github.com/the-control-group/voyager/issues/2969

    Hi,

    How can i get the user_id (logged user) on save?

    I want to know wich user add in table.

    Like this: https://github.com/the-control-group/voyager/issues/2969

  • Best Answer

    (As selected by hermesalvesbr)

    cookie 6 days ago
    Another possibility...
    Create a ``boot`` method on your Model with the follow content:
    
    ``
    
    	parent::boot();
    
    	static::creating(function($model)
    	{
    		$userid = (!auth()->guest()) ? auth()->user()->id : null;
    		$model->author_id = $userid;
    	});
    
    	//created
    	//updated
    	//deleted
    	//and so on .. ;-)
    ``

    Another possibility... Create a boot method on your Model with the follow content:

    ``

    parent::boot();
    
    static::creating(function($model)
    {
    	$userid = (!auth()->guest()) ? auth()->user()->id : null;
    	$model->author_id = $userid;
    });
    
    //created
    //updated
    //deleted
    //and so on .. ;-)
    

    ``

  • mark 6 days ago
    Let's imagine you have a Post model, that includes a `author_id` that you wish to automaticcally set when a post is saved, then you can inside the Post model add the following method:
    
    ```
        public function save(array $options = [])
        {
            // If no author has been assigned, assign the current user's id as the author of the post
            if (!$this->author_id && \Auth::user()) {
                $this->author_id = \Auth::user()->id;
            }
    
            parent::save();
        }
    ```

    Let's imagine you have a Post model, that includes a author_id that you wish to automaticcally set when a post is saved, then you can inside the Post model add the following method:

        public function save(array $options = [])
        {
            // If no author has been assigned, assign the current user's id as the author of the post
            if (!$this->author_id && \Auth::user()) {
                $this->author_id = \Auth::user()->id;
            }
    
            parent::save();
        }
    
  • cookie 6 days ago
    Another possibility...
    Create a ``boot`` method on your Model with the follow content:
    
    ``
    
    	parent::boot();
    
    	static::creating(function($model)
    	{
    		$userid = (!auth()->guest()) ? auth()->user()->id : null;
    		$model->author_id = $userid;
    	});
    
    	//created
    	//updated
    	//deleted
    	//and so on .. ;-)
    ``

    Another possibility... Create a boot method on your Model with the follow content:

    ``

    parent::boot();
    
    static::creating(function($model)
    {
    	$userid = (!auth()->guest()) ? auth()->user()->id : null;
    	$model->author_id = $userid;
    });
    
    //created
    //updated
    //deleted
    //and so on .. ;-)
    

    ``

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!