Globals are very similar to tables. While tables are made to contain values in multiple rows and multiple columns, a global is in fact one such value.
You can use globals for different use cases:
- When you need single values like a website title, some page intro, the company address, social media links, and so on.
- When you want to make a setting for something that is normally hardcoded in your code. This way it's also easily modifiable by your client.
- For labels (small pieces of text) you need through the site, especially in multilingual websites, like a 'Submit' label on a form button.
To avoid all your globals are in one big list, globals live in sets, for example labels
, address
, social_media
and so on. You first have to create a global set, and then create globals in it.
Every set and every global has a slug, which is important to access the global in code (see below).
Globals can be of any type, just like fields in tables.
Globals can also be translatable. Note that the list of globals is always presented with language columns, even if they are not translatable. This is because the table can be a mix of translatable and non-translatable globals, so this is the most convenient way to present this. After all, when you access a non-translatable global in code, you will get the same value in every language, so it does make sense.
Use the globals()
helper to access a global set. It returns an object on which you can request the individual globals:
<button type="submit">{{ globals('labels')->submit }}</button>