acoghlan's avatar

Setting $fillable on pivot table additional fields

I have a BelongsToMany relationship between two tables staff and systems, and a pivot table in the middle. The pivot table is populating properly, with everything working as per the doc.

I've added some additional fields to the pivot table - username, password, roll_id - so that when I link a staff member to a particular system I can store their auth credentials to access that system in the pivot table.

To populate those fields, I'm retrieving the entire data set and setting the fields as follows; $user = App\User->find(x); $pivot = $user->systems->find(x)->pivot; $pivot->system_username = 'uname'; $pivot->system_password = 'pwd'; etc...etc...etc... $pivot->save();

This is all working fine... BUT I dont seem to be able to set the fillable or guarded attributes for the pivot table, which means that there is a potential security risk. Given I'm populating these DB fields with data from a form, I'd like to be able to set the $fillable in this process.

I've tried creating a model called StaffSystem (the pivot table name is 'staff_systems' in the DB) but that doesnt get picked up.

Any thoughts? Are we even able to set a fillable parameter for pivot tables?

Thanks in advance

0 likes
2 replies
acoghlan's avatar

Thanks mstnorris - just what I was looking for.

1 like

Please or to participate in this conversation.