How do I traverse through model relationships in laravel with dot syntax

Ben Rowe

I'm trying to traverse through complex eloquent model relationships/attributes, and I'd like to use a simple dot structure to iterate through this, similar to how you can traverse arrays with Arr::get()

Example:

$data = [
  'foo' => [
    'bar' => [
      'key' => 'value'
    ]
  ]
];
$value = Arr::get($data, 'foo.bar.key'); // returns 'value'

I've tried using

$value = Arr::get($model, 'relation.subrelation.attribute')

However this fails and aways returns null, even though eloquent models support ArrayAccess.

Does laravel have a simple way to accomplish this?

Ben Rowe

For all those wondering, I've managed to figure out a solution by modifying the arr::pull() function to work specifically with models:

public static function traverse($model, $key, $default = null)
{
    if (is_array($model)) {
        return Arr::get($model, $key, $default);
    }


    if (is_null($key)) {
        return $model;
    }

    if (isset($model[$key])) {
        return $model[$key];
    }

    foreach (explode('.', $key) as $segment) {
        try {
            $model = $model->$segment;
        } catch (\Exception $e) {
            return value($default);
        }
    }

    return $model;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I use the dot syntax in Julia function calls

From Dev

How do I model multiple "many to many" relationships in Cassandra?

From Dev

How do I create a model that "belongsTo" a "hasMany through" join model?

From Dev

How can I traverse through my result?

From Dev

How does it works the Laravel 4 model relationships?

From Dev

How do I call a model in Laravel 5?

From Dev

Laravel: search through relationships?

From Dev

Loop through relationships Laravel

From Dev

How do I select which attributes I want for active model serializers relationships

From Dev

How do I traverse through a linked list after adding tails rather than heads

From Dev

Neo4j: how do I delete all duplicate relationships in the database through cypher?

From Dev

Laravel Category Model Relationships

From Dev

Reverse model relationships in Laravel

From Dev

How do I correctly hide model relationships from returning in toArray() or toJson()?

From Dev

Laravel model relationships and model events

From Dev

How do I traverse CakePHP relations?

From Dev

How do I traverse an array diagonally in javascript

From Dev

How do I traverse to the next position of an array

From Dev

How do I traverse CakePHP relations?

From Dev

Why eloquent model relationships do not use parenthesis and how do they work?

From Dev

How can I loop traverse a graph through const reference in D?

From Dev

How can I loop traverse a graph through const reference in D?

From Dev

Laravel5: How are Eloquent model relationships expressed in the database?

From Dev

Not able to specifically traverse through the syntax tree?

From Dev

Laravel: How do I create a custom pivot model?

From Dev

How do I add a custom field to a model in Laravel?

From Dev

How do I get the current model from custom method in laravel

From Dev

How do I load a Laravel Model using id and type name?

From Dev

How do I access a global model instance in laravel 4?

Related Related

  1. 1

    How do I use the dot syntax in Julia function calls

  2. 2

    How do I model multiple "many to many" relationships in Cassandra?

  3. 3

    How do I create a model that "belongsTo" a "hasMany through" join model?

  4. 4

    How can I traverse through my result?

  5. 5

    How does it works the Laravel 4 model relationships?

  6. 6

    How do I call a model in Laravel 5?

  7. 7

    Laravel: search through relationships?

  8. 8

    Loop through relationships Laravel

  9. 9

    How do I select which attributes I want for active model serializers relationships

  10. 10

    How do I traverse through a linked list after adding tails rather than heads

  11. 11

    Neo4j: how do I delete all duplicate relationships in the database through cypher?

  12. 12

    Laravel Category Model Relationships

  13. 13

    Reverse model relationships in Laravel

  14. 14

    How do I correctly hide model relationships from returning in toArray() or toJson()?

  15. 15

    Laravel model relationships and model events

  16. 16

    How do I traverse CakePHP relations?

  17. 17

    How do I traverse an array diagonally in javascript

  18. 18

    How do I traverse to the next position of an array

  19. 19

    How do I traverse CakePHP relations?

  20. 20

    Why eloquent model relationships do not use parenthesis and how do they work?

  21. 21

    How can I loop traverse a graph through const reference in D?

  22. 22

    How can I loop traverse a graph through const reference in D?

  23. 23

    Laravel5: How are Eloquent model relationships expressed in the database?

  24. 24

    Not able to specifically traverse through the syntax tree?

  25. 25

    Laravel: How do I create a custom pivot model?

  26. 26

    How do I add a custom field to a model in Laravel?

  27. 27

    How do I get the current model from custom method in laravel

  28. 28

    How do I load a Laravel Model using id and type name?

  29. 29

    How do I access a global model instance in laravel 4?

HotTag

Archive