Reputation: 73
I am currently a bit stuck with some data that my Vue component is sending to my Laravel Controller. The data is as follows:
array:2 [
0 => {#1246
+"id": 1
+"name": "Developer"
}
1 => {#1249
+"id": 2
+"name": "Ops Matrix Admin"
}
]
For example if I wanted to get the name
or the id
out of this as an object so that I can use it with eloquent. How would I go about doing this?
This is what my controller currently looks like.
public function editUserPermissions(Request $request, $id) {
foreach($request->all() as $key => $value) {
$decode = json_decode($value);
dd($decode);
}
}
When I do dd($request->all());
I get the following:
array:1 [
"body" => "[{"id":1,"name":"Developer"},{"id":3,"name":"Ops Matrix User"}]"
]
Upvotes: 1
Views: 2807
Reputation: 56
public function editUserPermissions(Request $request, $id) {
$bodys = $request->body;
foreach($bodys as $key => $body) {
//$key give current index of array
$body[$key]['id'] //this give id
$body[$key]['name'] //this give name
}
}
Upvotes: 0
Reputation: 463
You need to loop through the result. The result is an array.
A better way to get this would be with $request->getContent()
But using your code
public function editUserPermissions(Request $request, $id) {
foreach($request->all() as $key => $value) {
$decode = json_decode($value);
foreach($decode as $decoded) {
echo $decoded['id'];
}
}
}
Upvotes: 1