skip to Main Content

There is a controller method:

public function addEducation(AddPatientEducationRequest $request) {

    dd($request->all());

    $state = UserEducation::create($request->validated());
}

The dd() returns me an array that I need to insert into db:

array:1 [ // appHttpControllersApiPatientController.php:99
  "education" => array:1 [
    0 => array:5 [
      "patient_id" => 1
      "education_id" => 2
      "diplom" => "ZV 800"
      "start_date" => "2021-10-30"
      "end_date" => "2022-10-30"
    ]
  ]
]

I tried this way using loop:

foreach($request->education as $value) { UserEducation::create($value); }

2

Answers


  1. Your content looks like this:

    [
        'education' => [
            //some fields
        ]
    ]
    

    So, you can reach your record like this:

    myArray['education'][0]
    

    , hence your script should be

    foreach($request['education'] as $value) {
        UserEducation::create($value);
    }
    
    Login or Signup to reply.
  2. If education multidimensional array matches database columns then you can use the insert method like below

    UserEducation::insert($request->education)
    

    Ref :Insert Statements

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search