Website Settings in Laravel: in Database with Cache, or Package?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today I want to talk about global settings in your website for example if you want to customize some kind of footer text or header text or something like that like for example title so here I have a title based on our by the way Lara starters package so I've generated this kind of admin panel with user list and dashboard based on Windmill theme Tailwind theme with our free package called Lara starters and I will link to that in the description below but the topic of today is not the windmill theme it's actually customizing this text for example which is in the blade so in the navigation blade in my case we have this as an example it could be as I said footer text some repeating text on some section of the website and you want to customize that of course you can customize it by putting that into config so config app for example site title something like this but then it wouldn't be customizable by the users by the non-technical users so a better way is to have some kind of admin panel for them so they would be responsible for editing that themselves but the easier way is just to put that into the database why database and not config you would say that we still need a developer to fix that for example if manager told you that you need to change that text what is the difference that you change that in the config or in the database the difference is the deployment so either you need to commit the code test it merge and then deploy to the server or you just go to the database and change it so there's no code commit to change the text that's already one step further and of course on top of that on top of the database there should be some kind of admin panel if necessary but for now for this example let's take that one step and put this in the database so this is exactly what I did in this project I created a model called setting for any settings in that project so setting has fillable of key and value key is a string value is text just in case it could be longer and then with a database seater I've also seated one setting by default which is key site title and my website and here in the navigation blade we need to get that from the settings but it is quite a bad practice in the blade in the view to make database queries so where do we make that query from instead in app service provider you can Define this view share some kind of key and some kind of value the key is site settings which we'll use in the blade and the value comes from the database but cached this is pretty important because for header and footer there will be a lot of requests and there's a small chance that it will change so you can optimize the performance with caching that so you get setting all from the database then you get key by key which is a collection function then and then you cache that for an hour or whatever time period you want under the same key of site settings and then in your blade you have something like this so instead of that windmill you have side settings with the site title as the key because we use that key by collection method and then value would be from the database otherwise if it doesn't exist will still fall back to the windmill so now if we save refresh and we have my website instead and caching would make sure that if we change that in the database the database has this so if I change that to my websites for example this has changed in the database but I didn't refresh the cache clear the cache in laravel I refresh the website and it's still my website not my websites to refresh the cache you can do that in the code or manually by doing PHP Artisan cash clear and then if we refresh the page now we have my websites because it's taken from the database and cached again for an hour in our case for these simple settings it's totally fine and enough to have simple database table but I found out a few packages for more complex settings so there's a well-known spicy laravel settings which allows you to have separate settings class with more strict structure like string Bull and others so it has much more functionality with configuration and ability to save that data not only in the database but also in redis for example or another well-known package is laravel app settings from Q code and it provides also admin panel for managing the config so you define the settings here in your config and then you have something like this of course you need to protect that by middleware so only admins could access that but it's a visual layer on top of the settings database structure based by the way on bootstrap so that would work for you if you're a bootstrap user and you can use it without UI so the same package uses another package of the same vendor laravel settings which takes care of the database layer and then the laravel app settings provides the UI for that so these are two most well-known packages about the settings that I found online what is your experience where you stored the settings maybe you have some advice about some techniques for storing or caching in a different way or maybe some other packages share your experience in the comments below and subscribe to the channel to get daily videos also you can support the channel support my overall mission of educational content by going to laraveldaily.com and subscribing to yearly or monthly membership and then you will get all the premium courses and premium tutorials from here that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 12,440
Rating: undefined out of 5
Keywords:
Id: L99gtppYIMM
Channel Id: undefined
Length: 5min 40sec (340 seconds)
Published: Wed Dec 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.