skip to Main Content

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:

https://prnt.sc/wXMQc1zjHErI

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


  1. Chosen as BEST ANSWER

    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}", "AppHttpControllersLogoController");
    

    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


  2. By default, this line

    $path = request()->file("file")->store('public');
    

    saves the file in the directory storage/app/public.
    Laravel documentation says:

    (…) all files that should be publicly accessible should be placed in
    the storage/app/public directory. Furthermore, you should create a
    symbolic link at public/storage which points to the storage/app/public
    directory.

    Then the address returned by Storage::url($this->logo) will work.

    To create symlink you may use Artisan command:

    php artisan storage:link
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search