Olamilekan's avatar

Inserting multiple array into database in Laravel

Hello Good day everyone, Please i am trying to insert into my database multiple allowance and deduction when creating a new employee in my project.

Datas passed through to The AllowanceUser and DeductionUser model are not been saved. But user and account model datas are saved

Using dd($input) the datas passed through are shown for all model

Below is my current code

         //Creating User model Instance 
        $user = new User();


        // accepting User model datas
        $user->employee_name = $request->input('employee_name');
        $user->email = $request->input('email');
        $password = Hash::make($request->input('password'));
        $user->password = $password;
        $user->date_of_birth = $request->input('date_of_birth');
        $user->gender = $request->input('gender');
        $user->phone_number = $request->input('phone_number');
        $user->nationality = $request->input('nationality');
        $user->address = $request->input('address');
        $user->marital_status = $request->input('marital_status');
        $user->department_name = $request->input('department_name');
        $user->designation_name = $request->input('designation_name');
        $user->resumption_date = $request->input('resumption_date');
        $user->employee_status = $request->input('employee_status');


        //Checking if file upload is present 

        if ($request->hasFile('user_photo')) {
            // Get file name with extension
            $fileNameWithExtension = $request->file('user_photo')->getClientOriginalName();

            //Get file name only
            $fileName = pathinfo($fileNameWithExtension, PATHINFO_FILENAME);

            //Get file extension
            $fileExtension = $request->file('user_photo')->getClientOriginalExtension();

            $fileNameToStore = $fileName . '_' . time() . '.' . $fileExtension;

            //Upload image

            $imagePath = $request->file('user_photo')->storeAs('public/Employee_Images', $fileNameToStore);
        }

     
        
        //Saving User model Instance 
        
        $user->save();

    //getting all input using request all

        $input = $request->all();

        for ($i = 0; $i <= count($input['deduction_name']); $i++) {

            if (empty($input['deduction_name'][$i]) || !is_numeric($input['deduction_name'][$i])) continue;

            $data = [
                'allowance_id'=>$request->id,
                'user_id'=>$request->id,
                'deduction_value' => $input['deduction_value'][$i],
                'deduction_name' => $input['deduction_name'][$i]
            ];

            DeductionUser::create($data);
        }
        

        for ($i = 0; $i <= count($input['allowance_name']); $i++) {

            if (empty($input['allowance_name'][$i]) || !is_numeric($input['allowance_name'][$i])) continue;

            $data = [
                'allowance_value' => $input['allowance_value'][$i],
                'allowance_name' => $input['allowance_name'][$i]
            ];

            AllowanceUser::create($data);
        }
$account = new Account();
        $account->user_id = $request->id;
        $account->basic_salary = $request->input('basic_salary');
        $account->total_salary = $request->input('total_salary');
        $account->account_name = $request->input('account_name');
        $account->account_number = $request->input('account_number');
        $account->bank_name = $request->input('bank_name');
        $account->save();
0 likes
2 replies
Sti3bas's avatar

The AllowanceUser and DeductionUser model are not been saved.

Is it not saved at all? Do you get any errors?

Why are you passing the same value to both of these?:

'allowance_id'=>$request->id,
'user_id'=>$request->id,

user_id should probably be set to $user->id?

Also, make sure that this condition is not always true: if (empty($input['deduction_name'][$i]) || !is_numeric($input['deduction_name'][$i])) continue;

Olamilekan's avatar

I am not getting errors,


'allowance_id'=>$request->id, //(this should be getting the allowance id)
'user_id'=>$request->id,

is an error. but that is not the issue.

I am trying to save multiple allowance and deductions that relates to a particular employee.

allowance and deduction types are already saved in the database with an id.

An employee can have multiple allowance and deduction.

the AllowanceUser and DeductionUser Model is where i want the allowances and deductions corresponding to each user be stores

the allowance_user table has the following column

 Schema::create('allowance_user', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('allowance_id');
        $table->unsignedBigInteger('user_id');
        $table->string('allowance_name');
        $table->integer('allowance_value');
        $table->timestamps();
    });

Please or to participate in this conversation.