Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

crslp's avatar
Level 9

add user_id to a guarded model

I want to make use of the Model::create method. I declared the mass-assignable fields like so:

protected $fillable = [
      'name',
      'description',
      'value',
    ];

But, I want to store a user_id for each request. I want to set the user_id in my controller by using Auth::id(). This, however, does not work with the Model::create() method because I excluded the user_id from being fillable.

How to properly get around that issue? Thank you very much!

0 likes
10 replies
Talinon's avatar

I handle these situations in a couple ways. I usually add the user_id to the $fillable array, but ensure it always gets overridden either in the controller, or the model's creating() static boot method.

Controller method:

$request['user_id'] = auth()->id();
$model->create($request->all());

Model method:

    protected static function boot()
    {

        static::creating(function ($model) {

        $model->user_id = auth()->id();
            
        });    
    

    }

These ways, it doesn't matter what the client sends within the form, it always get overridden.

2 likes
crslp's avatar
Level 9

I like the second approach. But is it okay to override Laravel core files for this kind of action? What happens on an update?

Talinon's avatar

It's ok, you're not really overriding the core, but just hooking into the model events. For updates, you can similarly use the static updating() method:

 protected static function boot()
    {

        static::updating(function ($model) {

        $model->user_id = auth()->id();
            
        });    
    

    }
1 like
shez1983's avatar

if its not in the fillable you can always do it manually

$model->user_id = '';
$model->save();
crslp's avatar
Level 9

By adding this to my post model class

protected static function boot()
   {
       static::creating(function ($model) {

           $model->user_id = auth()->id();
           
       });   
   }

the only thing that gets saved is the user_id. What am I missing?

Edit: I figured it out by myself. Was missing the array-keys on the create([]) statement.

Snapey's avatar

Consider saving the model as a relation of the User, then you dont have to set the id at all... its done for you

Auth::user()->model()->create($request->all());
crslp's avatar
Level 9

Wouldn't this bloat my user-class with a ton of relations (by following this way on every Model)? Also, could this be a security issue? Connecting the user to every model?

But, I see the benefit in your approach...

Snapey's avatar

hey, your user model is connected - thats what you asked for..

What if you want to create the model and assign to someone other than the logged in user? With your creating method you are always hijacking the user_id.

1 like
crnkovic's avatar

@Talinon this is fine for tiny app such as your own blog where you will be the only user and no other user will interact with the site. As soon as you have more than one user, this is bad for the reasons that @Snapey said.

Anyways, to answer the question: The approach I like to take is by not using Model::create, but rather a new instance of the class. That way I have a bit more flexibility and don't have to care about foreign key naming:

$model = new Model($request->only(['field1', 'field2']));
$model->user()->associate($request->user());
// do anything else for the model
$model->save();
Talinon's avatar

@CRNKOVIC - It has nothing to do with the number of users, rather it uses the currently authenticated user; which may be fine, depending on the app. Snapey was stating that it may not be the best solution, IF you have the need to assign it to another user other than the one submitting the request.

There are plenty of ways to go about it.

Another option is to use forceFill() at an explicit end point, which forces the attributes to be mass assigned.

Please or to participate in this conversation.