Thursday, January 6, 2022

[FIXED] Laravel detect mass assigning

Issue

I have a model event defined on a model that each time a create a record I add the user's company id related to that record.

All works fine but I'm mass assigning records through csv importation and I must set the company id set as null.

How can I detect in Laravel when I'm massive assigning?

This is my model event:

protected static function boot()
    {
        parent::boot();

        static::creating(function (Product $model) {
            $model->company_owner_id = session()->get('currentCompany.id');
        });
    }

I've not done the controller's function that stores the records created through CRUD form neither the csv importer and both are strongly used by all the Laravel app so I have no chance to modify the importer or CRUD form.

Before using the csv importer the $fillable attribute on the model wasn't defined.


Solution

Finally I gave up on this and stopped to add the company_id on records as I can not determinate whether the records are created through csv importation or CRUD form.



Answered By - jcobo1

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.