dflydev/dot-access-data
Access and modify nested PHP arrays/objects using dot notation. Set, get, check, and append values with simple paths like a.b.c. Supports defaults and throws exceptions for missing paths—handy for configs and deep data structures.
Begin by installing the package via Composer: composer require dflydev/dot-access-data. Then instantiate a Data object—either empty or preloaded with an array—and use dot notation to access nested values. Your first real-world use case will likely be handling configuration from external sources (e.g., parsed JSON/YAML or .env dumps), where manual nested access ($config['db']['connection']['host']) becomes error-prone. Replace verbose isset() chains with simple, safe reads:
use Dflydev\DotAccessData\Data;
$config = new Data(json_decode(file_get_contents('config.json'), true));
$host = $config->get('database.connections.mysql.host', 'localhost');
This avoids notices and keeps code readable—especially useful when working with loosely structured or inconsistent external configs.
Data to provide a stable, dot-accessible interface. Ideal for multi-environment setups (local/staging/production) where config keys may vary.append() to safely build arrays without existence checks—e.g., accumulating user roles from multiple sources:
foreach ($roles as $role) {
$data->append('user.permissions', $role);
}
getData() to retrieve nested Data instances for modular handling (e.g., passing a specific service config to a bootstrapper without exposing the full object):
$mailConfig = $data->getData('mail.providers.smtp');
ArrayAccess for clean syntax in views:
<p>{{ $config['app.debug'] }}</p>
/ for paths derived from external systems (e.g., Firebase-style paths or REST API filters) and . for internal consistency—no conversion needed.get() throws MissingPathException if the path is missing and no default is provided. Always supply defaults for optional or external inputs:
$enabled = $data->get('feature_flags.ui_updates', false); // safer
null is a Value: A key set to null (e.g., $data->set('key', null)) exists (has() → true), whereas a truly missing path (has() → false) throws on get(). Use has() to distinguish:
$data->set('enabled', null);
$data->has('enabled'); // true
$data->get('enabled'); // null
append() merges only indexed arrays—associative arrays get replaced, not merged. Use merge() for associative merging:
$data->set('opts.x', ['a' => 1]);
$data->merge('opts.x', ['b' => 2]); // ✅ ['a' => 1, 'b' => 2]
. and / (e.g., 'a.b/c') throws InvalidPathException. Stick to one delimiter per path for clarity.has() over get() with defaults in assertions—it avoids catching exceptions just to check existence. Use getData() to test sub-objects without re-parsing:
$this->assertTrue($data->has('db.credentials'));
$this->assertEquals('localhost', $data->getData('db')->get('host'));
How can I help you explore Laravel packages today?