R
R
Roman Sarvarov2020-04-18 12:11:17
Laravel
Roman Sarvarov, 2020-04-18 12:11:17

Dot notation in working with arrays in laravel. Is it worth using?

There are many helpers in laravel related to working with arrays.
One group of these helpers is dedicated to dot notations (eg arr::has, arr::get, etc.).

Basically, the appearance of the code becomes more readable, as it seems to me:

Arr::has($array, ['product.data.price', 'product.data.name']);

instead of
isset($array['product']['data']['price'], $array['product']['data']['name']);


Is it worth it to work with arrays this way throughout the project?
Or can you give an example of a situation where you can resort to dot notation (or why was it done at all in this case?). I just have not come across this in any way except with the blade.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2020-04-18
@megakor

Purely my IMHO. This is not a standard.
If you can not use helpers - in principle, you should do it, but this applies to operators in the language ( []), rather than standard functions (which is isset()).
So that:

Arr::has($array, 'nested.key') а не isset($array['nested']['key'])
Arr::get($array, 'nested.key') а не isset($array['nested']) ? ($array['nested']['key'] ?? null) : null
$array['key'] ?? null а не Arr::get($array, 'key')
$array['key'] ?? doSomething() а не Arr::get($array, 'key', fn () => doSomething())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question