0

I'm a beginner and currently learning Vue js and Laravel. So I'm trying to figure out how to create a single POST request via axios then insert multiple entries into my database.

I have this array for example:

{
  "comakers": [
    {
      "name": "Sample",
      "email": "[email protected]"
    },
    {
      "name": "Test",
      "email": "[email protected]"
    }
  ]
}

then on my laravel controller I want to do a foreach loop like this

public function update(Request $request, $id)
{
    $data = $request->all();

    foreach ($data as $comaker){
        $nominate = new Nominate();
        $nominate->loan_application_id = $id;
        $nominate->comaker_name = $comaker->name;
        $nominate->comaker_email = $comaker->email;

        $nominate->save();
    }

}

But I get an error "Trying to get property 'name' of non-object". Can someone please give me some idea on how to achieve this?

And btw I'm trying to use the update function from my controller. Basically, I want to (insert new if not existing) or (edit when theres an existing data). Am I doing the right approach here?

(UPDATE)

laravel dd or dump shows this when I try to get the value of $data = $request->all();

array:1 [
  "comakers" => array:2 [
    0 => array:2 [
      "name" => "Sample"
      "email" => "[email protected]"
    ]
    1 => array:2 [
      "name" => "Test"
      "email" => "[email protected]"
    ]
  ]
]
4
  • Error messages like that normally have a line number to give you a clue where to find the error. Can you show ALL the error message when you post here please Commented Dec 12, 2018 at 0:43
  • Hi Noah, can you update your question with the axios post, I think you're missing to find the array in the request Commented Dec 12, 2018 at 0:43
  • 1
    Do a var_dump($data) and show us the output please Commented Dec 12, 2018 at 0:45
  • @RiggsFollyI added the dump logs on question Commented Dec 12, 2018 at 0:51

3 Answers 3

2

I don't think all() will return an array of objects but instead an associative array. So you could try the following:

// Notice that you should use `json()` since the data is in json format
$data = $request->json()->all();

foreach ($data['comakers'] as $comaker) {
  $nominate = new Nominate();
  $nominate->loan_application_id = $id;
  $nominate->comaker_name = $comaker['name'];
  $nominate->comaker_email = $comaker['email'];

  $nominate->save();
}

of course you could also cast the result to an object like this:

foreach ($data['comakers'] as $item) {
  $comaker = (object) $item;
  $nominate = new Nominate($data);
  ...

I would also suggest that you simply print out the contents of the result in order to see what it actually contains, using var_dump() for instance:

var_dump($data);
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Cyclonecode, I added the line $comaker = (object) $comaker; after the foreach and it actually solved the problem. Can you add it back so I can accept your answer? Thanks a lot for the help!
@Noah - Yes I added it again, but the only reason that is working is because the data is not an array of object as I stated at the top of my answer. The data is an associative array, so the first solution should work as well.
1

You are forgetting the comaker key when you try to loop, you should do it like this

$data = $request->all();

foreach ($data['comakers'] as $comaker){
    $nominate = new Nominate();
    $nominate->loan_application_id = $id;
    $nominate->comaker_name = $comaker['name'];
    $nominate->comaker_email = $comaker['email'];

    $nominate->save();
}

2 Comments

I don't think this will work, since there are no objects but associative arrays in the result.
You are right, Laravel decodes json as associative array, I will update my answer
0

I think your doing a foreach on the Json variable not the comakers object inside it.

foreach ($data->comakers as $comaker) {
        $nominate = new Nominate();
        $nominate->loan_application_id = $id;
        $nominate->comaker_name = $comaker->name;
        $nominate->comaker_email = $comaker->email;

        $nominate->save();
}

2 Comments

Unfortunately, doing this shows another error "Trying to get property 'comakers' of non-object" instead
Right, I saw your vardump, you cant get the property of a non-object. Since its an Array, you should try $data['comakers'] for the foreach and $comaker['name'] inside the loop

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.