0

When i am passing an associative array of data to controller its showing error that 'trying to get non-object property' my controller function is,

        public function storeWBS(Request $request) {
        foreach($request->all() as $value){
            $formValue                = new WorkBreakdownStructure;
            $formValue->form_field_id = $value->idea_id;
            $formValue->value         = $value->wbs_description;
            $formValue->save();

        return back();}

    }

i have set the variables 'fillable' in model

'id' ,'form_field_id' ,'value' if i print the '$formValue' with DD() i am getting the result

#parameters: array:4 [▼
  "_token" => "u6iwKspevWLiuI6CRPhd82c8xm0EYb6IGQQJX2aR"
  "idea_id" => array:3 [▼
    0 => "1"
    1 => "1"
    2 => "1"
  ]
  "wbs_description" => array:3 [▼
    0 => "Work Breakdown Structure #1 :"
    1 => "Work Breakdown Structure #2"
    2 => "Work Breakdown Structure #3"
  ]
  "percentage" => array:3 [▼
    0 => "50"
    1 => "12"
    2 => "12"
  ]
]

1 Answer 1

1

all() returns array, not object so you cannot access it as you do now:

.... = $value->idea_id;

Instead you need to change this and use array syntax:

.... = $value['idea_id'];

EDIT

You also should remove forach() because you basically iterate over the array which makes no sense:

public function storeWBS(Request $request) {
      $value = $request->all();
            $formValue                = new WorkBreakdownStructure;
            $formValue->form_field_id = $value->idea_id;
            $formValue->value         = $value->wbs_description;
            $formValue->save();

        return back();
}

Also you need to fix return back(); line. whatever back() refers to, you most likely invoking it wrong.

Sign up to request clarification or add additional context in comments.

4 Comments

Dear @Marcin Orlowski i also try this but its returning error Illegal string offset 'idea_id'
What you see when you do var_dump($value); or DD($value); in your loop?
i am getting only the token value not an array don't know whats wrong "u6iwKspevWLiuI6CRPhd82c8xm0EYb6IGQQJX2aR @Marcin
if i remove for loop then i am getting grammar error Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given,

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.