0
0
0ldn0mad2019-08-06 20:53:45
Laravel
0ldn0mad, 2019-08-06 20:53:45

What is the correct way to write an array to a Laravel controller?

How to correctly write an array with associative keys as a Laravel controller parameter, so that later it can be passed to the viewer - {{ $arr['name'] }}, {{ $arr['age'] }}, {{ $arr['salary' ] }}?
Array example:

$arrUsers = [
    ["name" => "Вася"],
    ["age" => 33],
    ["salary" => 450]
];

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2019-08-07
@0ldn0mad

In the controller

$users = [
     [
      "name" => "Вася",
      "age" => 33,
      "salary" => 450
     ]
 ];
return view('users', ['users' => $users]);
// или
return view('users', compact('users'));

in the users template
@foreach ($users as $user)
        {{$user['name']}}
@endforeach

G
Grigory Vasilkov, 2019-08-07
@gzhegow

The entire array must be inserted into the arr key. There's an export function under the hood

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question