Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

idcreatv's avatar

Inserting directly into a pivot table - error 500 - stumped!

Following on from my other threads with @gustav1105 (http://bit.ly/2qngvdL - best way of handling a relationship) and another where I was trying to get Vue and Blade working together (http://bit.ly/2rl7VKj - I am now using jQuery), I've now got my Ajax connections working and am back to this final part where I'm simply adding the results to my course_employee table.

Using Ajax I'm sending to the server the employee_id, employer_id and course_id which I need to put into the pivot table to make the connections between employees and courses.

At first I was testing and just returned the following:

return 'ADDED: employee '.$employeeid.', employer '.$employerid.', course '.$courseid;

I console logged it and I could see that the correct IDs were being sent correctly so I know I'm sending everything up without any issues.

Next I added my insert code so it could add everything to the database. This is the first time I've done this without using Eloquent and I'm hitting an error.

This is what I'm using to insert into the table:

public function addAttendee(Request $request, $employeeid, $employerid, $courseid){

        DB::table('course_employee')->insert([
            'course_id' => $courseid,
            'employee_id'=> $employeeid,
            'employer_id'=> $employerid
            ]);
        // The below is logged to console so I can check that the correct Ids are being used - they are.
        // return 'ADDED: employee '.$employeeid.', employer '.$employerid.', course '.$courseid;
    // Grab the employee/attendee's details so I can add and style the attendee table
    $attendeeResponse = Employee::find($employeeid);
        return response()->json($attendeeResponse,200);
    }

And this is the error I get in console in response:

[Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. at Function.remoteFunction (<anonymous>:2:14)]

My Course model looks like this:


The reason I've not used Eloquent is that as per my original post (http://bit.ly/2qngvdL) I'm not creating an 'attendee', just putting the link in between course, employee and employer. I've added entries manually in Sequel Pro and it returns the attendees fine, so I know the relationships are working - its just getting Laravel to add the three IDs to the pivot table and getting the successful response where I'm hitting an issue.

Any help would (as usual) be most appreciated!
0 likes
1 reply
idcreatv's avatar
idcreatv
OP
Best Answer
Level 3

Edit: after spending ages looking at it I found the problem - I was using:

$attendee = DB::table('course_employee')->insert(
            array('employee_id' => $employeeid,
                  'employer_id' => $employerid,
                  'course_id' => $courseid)
        );

Instead of:

$attendee = \DB::table('course_employee')->insert(
            array('employee_id' => $employeeid,
                  'employer_id' => $employerid,
                  'course_id' => $courseid)
        );

See the difference? No? Its the slash before DB - it should be \DB not DB!

1 like

Please or to participate in this conversation.