Olamilekan's avatar

Populating Many to Many Intermediate Table with Array input

Please I am trying to insert multiple deduction name and it corresponding values into my database .

the intermediate table has the following coloumn user_id, deduction_id, deduction_name, deduction_value

The deduction name and value inputs are been dynamically generated using JQuery.

I get the below error ... I am unable to get the deductions id and get the deduction_name and deduction_value data

Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'deduction_name' doesn't have a default value (SQL: insert into `deduction_user` (`created_at`, `deduction_id`, `updated_at`, `user_id`) values (2020-03-30 19:18:22, 5, 2020-03-30 19:18:22, 5))


  public function store(Request $request)
    {

   

        //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();


        $deduction = Deduction::all();

        

        $user->deductions()->attach($user->id);


        $user->deductions()->attach($request->input('deduction_name'));
        $user->deductions()->attach($request->input('deduction_value'));

        $user->deductions()->attach($deduction->id);



       


    }

0 likes
2 replies
bobbybouwmann's avatar

The current error is pretty clear. You're trying to insert data, but you didn't fill the deduction_name column. The problem lies in the attach method. The attach method first expects the id of the deduction and then all the extra fields for the pivot table

$user->deductions()->attach($deduction->id, [
    'deduction_name' => $request->input('deduction_name'),
]);

Documentation: https://laravel.com/docs/7.x/eloquent-relationships#updating-many-to-many-relationships

Olamilekan's avatar

Oh, I get this, I am not trying to update the table, I am inserting data into the table and i have tried this.

I still got this error

SQLSTATE[HY000]: General error: 1364 Field 'deduction_name' doesn't have a default value (SQL: insert into `deduction_user` (`created_at`, `deduction_id`, `updated_at`, `user_id`) values (2020-03-30 21:42:15, 6, 2020-03-30 21:42:15, 6))

I noticed the user id is been assigned to deduction id as well.

Then my deduction name and value input are been generated dynamically i.e a user can have more than one deductions

NOTE: my deductions are been loaded to the user interface from the database and each has an ID. I can access see the id on the user interface, but i have not been able to access in my store function



        $deduction = Deduction::all();

        

        $user->deductions()->attach($user->id);

        $user->deductions()->attach($deduction->id, [
            'deduction_name' => $request->input('deduction_name'),
            'deduction_value'=> $request->input('deduction_value')
        ]);



Please or to participate in this conversation.