I’ve setup company-scoped routes, such as
Each user may have access to multiple companies, so the path determines the scope, making sure that I will only load products for company A when visiting the first url.
I have set up n auth helper auth()->company()
which will simply return the current company, based on the path, so I can do things like: auth()->company()->products()
// return the current company via auth()->company() or Auth::company()
SessionGuard::macro('company', function () {
return $this->request->company;
});
I tried setting up a Livewire component for the products list, but noticed that it only works on the inital page load. On ajax calls, the scope is gone, because the url that livewire calls (myapp.com/livewire/message/products-list
) does not contain the company slug anymore, meaning that I will get an error like
Call to a member function products() on null
I tried passing the company as a param to the products-list component:
<livewire:products-list :company="auth()->company()">
and in the component, using it:
public function render()
{
return view('livewire.products-list', [
'products' => $this->company->products()
->orderBy('name')
->paginate()
]);
}
but again, this only works on the inital pageload.
Essentially, I would need to either customize the livewire url, so it’s prefixed with the company slug, or find another way to pass the current company scope to livewire. I’d prefer if I could just call auth()->company()
though.
Any ideas?