Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Dot Access Data Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Configuration Layer Abstraction: Wrap your app’s raw configuration arrays in Data to provide a stable, dot-accessible interface. Ideal for multi-environment setups (local/staging/production) where config keys may vary.
  • Dynamic Data Accumulation: Use 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);
    }
    
  • Subtree Extraction: Use 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');
    
  • Blade Integration: Leverage ArrayAccess for clean syntax in views:
    <p>{{ $config['app.debug'] }}</p>
    
  • Hybrid Delimiter Support: Use / for paths derived from external systems (e.g., Firebase-style paths or REST API filters) and . for internal consistency—no conversion needed.

Gotchas and Tips

  • Default vs. Missing Key: In v3+, 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
    
  • Associative Array Quirks: 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]
    
  • Path Sanity: A single path string mixing . and / (e.g., 'a.b/c') throws InvalidPathException. Stick to one delimiter per path for clarity.
  • Testing: Prefer 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'));
    
  • PHP 8.4 Compatibility: Versions ≥3.0.3 fix deprecation notices—ensure you’re using the latest patch release for modern PHP compatibility.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests