shaungbhone's avatar

Upload logo

Now, I am stuck with another developer's code. I want to show at the blade file the uploaded logo in this path.

Controller.php

public function doUploadLogo()
    {
        $file = $this->request->file('logo-image');

        $allowedFileExtensions = ['png', 'jpg', 'gif'];

        if ($file != null && $file->isValid()) {
            $fileExtension = strtolower($file->getClientOriginalExtension());
            if (in_array($fileExtension, $allowedFileExtensions)) {
                $newFilename = getLogoName();
                //Storage::disk('public')->put('templates/common/images/logos/' . $newFilename, File::get($file));
                $file->move(Storage::path('templates/common/images/logos', $newFilename));
                if ($file->getError() === 0) {
                    return redirect()
                        ->back()
                        ->withSuccess('Logo uploaded');
                }
            }
        }

        return redirect()
            ->back()
            ->withErrors(['Upload Failed']);
    }

allfunction.php

function getLogo()
{
    $logoName = getLogoName();
    $fullPath = Storage::path('templates/common/images/logos/' . $logoName);

    if (file_exists($fullPath)) {
        return url('templates/common/images/logos/' . $logoName);
    } else {
        return url('templates/common/images/logos/logo.jpg');
    }
}

header.blade.php <- it does not show. Where am I wrong?

<img style="height: 100%;" src="{{ getLogo() }}" alt="" />
0 likes
2 replies
drehimself's avatar
Level 35

You need to break down your problem further to see where things are not working.

First, check if the upload works. Upload a file through the UI and check the storage path to see if the file is actually in there. If it is, you know your upload works. If not, there's something wrong with the upload.

For showing the logo (assuming upload works and the file is in the correct spot), you can dd the return values and manually check if that URL works in the browser. If it doesn't there's an issue with the logic for getting the correct URL.

Happy debugging!

2 likes
shaungbhone's avatar

Thank you @drehimself. Now it's worked. You are my teacher since you are on youtube. I learned a lot from you.

1 like

Please or to participate in this conversation.