someone? Please. It is an important feature that is missing in this project, and for now, the only one that we can't find a solution
Multitenancy and Storage - Getting Image from tenant
Since I started with multi tenant, I'm having problems getting the url path for images
My tenants can upload an image. Lets say its the logo With multi tenant it saved the folders like in this image:
I save the file using the following line code:
$path = request()->file("file")->store('public');
Its automatic the generation of the tenant folder, the documentation explains that: https://tenancyforlaravel.com/docs/v2/filesystem-tenancy/
I save the path at my database. It saves a line as the following example:
public/JwsCeCCxgKiM8ZVYAMNt9gPJeZKDsb8NUKmPzak8.jpg
Now I want to get the URL of that file, in order to load the logo in my front application:
private function getLogoPath(){
if($this->logo == null)
return null;
return asset(Storage::url($this->logo));
}
I receive the message
Tenant could not be identified on domain localhost
It was working without multi tenant. Now I'm having troubles because of multi tenancy
Can someone help me in order to know what to do or how can I do it?
As an answer, I'm returning the URL
private function getLogoPath(){
if($this->logo == null)
return null;
return \URL::to(tenancy()->tenant->id . "/storage/logo/" . $this->id);
}
I use tenancy to identify the tenant where I want to go and then, and this is importantn, I created a controller:
class LogoController extends Controller
{
public function __invoke(Request $request){
$file = Storage::path($file->logo);
return response()->file($file);
}
}
And this controller runs inside a tenant, because in the web.php inside the route folder I have this:
\Route::name("logo.get")->middleware(["auth", InitializeTenancyByPath::class])
->get("{tenant}/storage/logo/{file_id}", "\App\Http\Controllers\LogoController");
As we can see, when I return the URL, the front makes another request to the returned URL. That request is executed by that route and, the TENANT is important. Because using the initialization by tenant, we can go inside the tenant storage folder and after that we can work normally
Please or to participate in this conversation.