This is my first question so please excuse any protocol errors!
I am also new to Laravel 5. I have watched the Fundamental video series (especially "syncing tags" and have that working for simple pivot tables) and have searched the forum and further online but am not actually even sure if what I am searching for is the correct way to go about achieving what I wish.
I am trying to create/update/edit a user's profile to track their work experience. So a user can have multiple positions and those positions are to be linked to companies.
I would like the company table to be separate so that users can select existing companies from the dropdown. And the same for position - if a position exists, such as "CEO", I would like the user to be able to select that instead of adding in a new one.
I created a form with fields as below:
<div class="workexperience">
<!--Work Experience / position -->
<!-- Form Input -->
<div class="form-group">
{!!Form::label('positions_list', 'Work Experience:')!!}
{!!Form::select('positions_list', $positions, null, ['id'=> 'positions_list', 'class'=>'form-control','multiple'])!!}
</div>
<!--company-->
<div class="form-group" >
{!!Form::label('companies_list', 'Company:')!!}
{!!Form::select('companies_list', $companies, null, ['id'=>'companies_list','class'=>'form-control','multiple'])!!}
</div>
</div>
<div id="workexperience-placeholder"></div>
<a onclick="cloneMe('#workexperience')" class="btn btn-default btn-block"><span class="icon-entypo icon-plus"></span><b>Add Another</b></a>
and am using jquery clone to allow the user to add in new sets of positions and companies
function cloneMe(thisDiv)
{
$(thisDiv).clone(false).insertBefore(thisDiv+"-placeholder:last");
}
This visually does what I want but is not posting an array of positions and companies as I would have expected.
So,what I am struggling with is how to save/ sync users, positions and companies.
I created pivot tables for company_user, position_user and company_position as below:
company_user:
company_id
user_id
position_user
position_id
user_id
company_position
company_id
position_id
and had imagined a scenario as follows:
User is created,
Positions are linked to user (using sync method)
Companies are linked to user (using sync method)
positions are linked to companies(?)
But I can't work out how to sync positions and companies since they might need to be created first. Basically trying to work out how to retain the relationship between positions and companies, especially since there could be multiples of those per user.
EDIT
So I am trying the suggestion of adding in a company_ID to the position_users table.
What I can't work out is how to sync the info coming in from position and company fields, especially since the position or company may or may not currently exist on the database.
I am using withPivot:
public function users()
{
return $this->belongsToMany('App\User')->withPivot('company_id')->withTimestamps();
}
to add in the company_id field to the pivot table. But I am not sure how to add in that id field to the array when it has to be potentially created first.
Following Laracasts - 23 - Syncing Tags, I am using
$user->positions()->sync($this->processPositions($positions));
Where the processPositions function returns an array of all associated id's for positions coming from the form - whether they previously existed or are newly created.
However I am not seeing how to add the company_id to this sync function.
Just a note that the position and company fields can be cloned so there are potentially multiple "sets" of positions and companies.