To work with the GlobalSet Repository, use the following Facade:
use Statamic\Facades\GlobalSet;
Methods
Methods | Description |
---|---|
all() |
Get all GlobalSets |
find($id) |
Get GlobalSets by id |
findByHandle($handle) |
Get GlobalSets by handle |
findOrFail($id) |
Get GlobalSets by id . Throws a GlobalSetNotFoundException when the global set cannot be found. |
make() |
Makes a new GlobalSet instance |
Querying
Globals are a bit unique in that there is a single "container" – the Global Set – which contains common things like its title and handle. The actual variables are stored separately by site (even when not using the multi-site feature).
$set = GlobalSet::findByHandle('theme'); // returns a `GlobalSet`$variables = $set->in($site); // returns a `Variables`$variables->get('favicon'); // returns the value
Of course, you can chain this:
GlobalSet::findByHandle('theme')->in($site)->get('favicon')
You must specify your site when working with GlobalSets, even if you only have one. You can use any of these methods:
$set->in('siteHandle'); // A specific site handle from sites.php$set->inDefaultSite(); // The first site in sites.php$set->inCurrentSite(); // The site the user is currently visiting.
Creating
Start by making a global set instance with the make
method. You can pass in the handle.
$globals = GlobalSet::make('settings')->title('Settings');
The variables are stored per-site in a Variables
object. You can make one using the makeLocalization
method, passing in the site handle. Then attach it to the set.
$variables = $globals->makeLocalization('default');$variables->data($data); // array of values$globals->addLocalization($variables);
Finally, save it.
$globals->save();