Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 3771
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T01:56:57+00:00 2022-06-03T01:56:57+00:00

Laravel 7 – Bulk Insertion

  • 0

[ad_1]

I’m trying to insert the selected data with checkboxes but when I submit the dat it throws me this error:

Invalid argument supplied to foreach().

It worked before when I had the first foreach and the variable $consecutive_final

  • My function that does the array insert of the selected checkboxes and request the data
 public function create(Request $request)
    {
        try {
            foreach ($request->get('student_id') as $index => $student_id) {
                foreach ($request->get('anio') as $index => $anio) {
                    foreach ($request->get('season') as $index => $season) {
                        foreach ($request->get('program') as $index => $program) {
                            foreach ($request->get('course') as $index => $course) {
                                foreach ($request->get('level') as $index => $level) {
                                    foreach ($request->get('grade') as $index => $grade) {
                                        $consecutive = DB::select('SELECT SUM(certificationId) FROM certifications GROUP BY certificationId');
                                        $final_consecutive = sprintf("%04d", $consecutive);
                                        Certifications::create([
                                            'student_id' => $student_id,
                                            'consecutive' => $final_consecutive,
                                            'year' => $year,
                                            'season' => $season,
                                            'program' => $program,
                                            'course' => $course,
                                            'level' => $level,
                                            'grade' => $grade,
                                        ]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return back()->with('success', 'Constancia creada correctamente');
        } catch (\Illuminate\Database\QueryException $e) {
            $message = $e->getMessage();
            if (strpos($message, "Duplicate entry")) {
                return back()->with('err', 'Esta constancia ya ha sido creada');
            }
            if (strpos($message, "1366 Incorrect integer value: '' for column 'idGrupo'")) {
                return back()->with('err', 'Debe seleccionar un grupo para poder continuar');
            }
            return back()->with('err', $message);
        }
    }
  • And this is my form where the inputs don’t actually ask for information, but I map it
<form action="{{url('/Create/Certification/')}}" method="POST">
                                {{csrf_field()}}

                                <td class="align-middle text-center">
                                <input class="form-control form-control-sm" type="text" name="grade[]" 
                                value="{{isset($student->pivot->grade)?$student->pivot->grade:''}}" placeholder="grade" disabled>
                            </td>

                            <td class="align-middle text-center">
                            @foreach ($group->levels as $level)
                                <input type="hidden" value="{{ $nivel->course->program->season->year->year }}" id="year" name="year[]">
                                {{$level->course->program->season->year->year}}
                            @endforeach
                            </td>
                            <td class="align-middle text-center">
                            @foreach ($group->levels as $level)
                                <input type="hidden" value="{{ $level->course->program->season->seasonName }}" name="season[]">
                                {{$level->course->program->season->seasonName}}
                            @endforeach
                            </td>
                            <td class="align-middle text-center">
                            @foreach ($group->levels as $level)
                                <input type="hidden" value="{{ $level->course->program->programName }}" name="program[]">
                                {{$level->course->program->programName}}
                            @endforeach
                            </td>
                            <td class="align-middle text-center">
                            @foreach ($group->levels as $level)
                                <input type="hidden" value="{{ $level->course->courseName }}" name="course[]">
                                {{$level->course->courseName}}
                            @endforeach
                            </td>
                            <td class="align-middle text-center">
                            @foreach ($group->levels as $level)
                                <input type="hidden" value="{{ $level->levelName }}" name="level[]">
                                {{$level->levelName}}
                            @endforeach
                            </td>

                        

                                <input class="form-control form-control-sm" type="hidden" name="student_id[]" value="{{$student->student_id}}" >

                                <td class="align-middle text-center">
                                    <input id="select" type="checkbox" name="select[]">
                                </td>
                        </tr>
                        @endif
                        @endforeach
                    </tbody>
                </table>
                    <div class="d-flex justify-content-center">
                        <button type="submit" class="btn btn-sm btn-outline-success">Create</button>
                    </div>
                </form>

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8057 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1842 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1924 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.