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 Storage::url($this->logo);
}
It is impossible to load that, because it will return the path stored in the database
If I try to do: localhost:8000/ returned_path
Its not working because I cant find any image
It was working without multi tenant. Now I’m having troubles because of multi tenancy
Tried already with
private function getLogoPath(){
if($this->logo == null)
return null;
return asset(Storage::url($this->logo));
}
it returns the message:
Tenant could not be identified on domain localhost
Can someone help me in order to know what to do or how can I do it?
UPDATE:
With this code I’m able to get the correct path inside my laravel server:
private function getLogoPath(){
if($this->logo == null)
return null;
//Storage::disk('tenancy')->delete($this->logo);
return Storage::disk('tenancy')->url($this->logo);
}
I know this because if I run the delete, it deletes the image inside the correct folder.
Now, I need to return an url. Because this is returning to a vuejs api to load the image
2
Answers
As an answer, I'm returning the URL
I use tenancy to identify the tenant where I want to go and then, and this is importantn, I created a controller:
And this controller runs inside a tenant, because in the web.php inside the route folder I have this:
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
By default, this line
saves the file in the directory storage/app/public.
Laravel documentation says:
Then the address returned by
Storage::url($this->logo)
will work.To create symlink you may use Artisan command: