Hi @adgower
I'm not sure if I understand your question, but it seems to me that you are trying to have some sort of Multi-Tenancy in place?
If that's the case, there are some packages that will make that easy.
Or you can follow this tutorial (from Povilas Korop) to have a basic multi tenancy model: https://youtu.be/nCiNqboYFVQ
Basically, you have to create a Multitenantable Trait with something like this:
trait Multitenantable {
public static function bootMultitenantable() {
static::creating(function($model) {
$company = Auth::user()->company;
$model->company_id = $company->id;
});
static::addGlobalScope('belongs_to_company_id', function(Builder $builder) {
$company = Auth::user()->company;
return $builder->where('company_id', $company->id);
});
}
}
And the use that Trait on any model that you want to automatically filter by the User's Company ID. This will also automatically assign the User's Company ID for any new instance of the Multitenantable Models