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.
CodePudding user response:
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.
CodePudding user response:
You can take your model and add the array ['company_owner_id'] to the fillables for this model. If you put your creating logic in the import controller rather than the service provider, it will only run from there.
protected static function boot()
{
parent::boot();
static::creating(function (Product $model) {
$now_fillable=['company_owner_id'];
$model->mergeFillable($now_fillable);
$model->company_owner_id = session()->get('currentCompany.id');
});
}
Happy Coding!
