1

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"}]"
]

2 Answers 2

1

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'];
        }
    }
  }
Sign up to request clarification or add additional context in comments.

7 Comments

Hi, Thanks for answering. Currently I have already used json_decode and that is how I retrieved the dataset that I have put in my question. I do apologise if I was not clear. Have a nice day!
Hi thank you. I have updated my question. Again apologies if I am being completely stupid. This problem has consumed me for about 2 days now :D
Hmmm I seem to be getting an error "{message: "Undefined index: id", exception: "ErrorException",…}" I feel really bad because I should of figured this one out.
Answer updated to include your updated code. I am assuming the first bit is dd($decode)
Thank you so much... Seriously, I have been banging my head off of a wall for a while. This has really helped. I now just need to make it comma separated, but I will check on that. You have done more than enough!
|
0
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 
      }
  }

4 Comments

Hi :D With this solution to my problem I received the following error: {message: "json_decode() expects parameter 1 to be string, object given", exception: "ErrorException",…}
I have updated my question with the output from dd($request->all());
$request->all() should be in there my fault.sorry
Ahh I see. New error for our pleasure. Sorry for bothering you with this. I really do appreciate the help. {message: "Cannot use object of type stdClass as array", exception: "Error",…}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.