distantplanet's avatar

Admin Customizable Background IMG

what's the basic approach for having a admin customizable background image?

i have a form set up to upload a new background image and save the name to a db.

now i'm wondering where i connect the database call for the image name to the CSS so it works on every page.

0 likes
1 reply
bobbybouwmann's avatar

I would probably write a helper function it in some kind of helper file and then you create your function in that file and call it in your view, so something like this should do

// app/Http/helpers.php

function getBackgroundImageForUser($userId)
{
    $user = User::findOrFail($userId);

    return $user->background_image; // This is the path to the file
}

Now you can do this in your view

Now to make this work you need to autoload the file first in your composer.json

// composer.json

"autoload": {
    "files": [
        "app/Http/helpers.php"
    ],
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

You can use this file to add other functions that you want to use on the front end and back end as well ;)

1 like

Please or to participate in this conversation.