0

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.

Mhluzi Bhaka
  • 1,364
  • 3
  • 19
  • 42
  • Seems that many companies can have many positions. If we agree that a user can have a position in a company only once (cant be CEO twice in the same company), then instead of having `company_user` table you could add a `company_id` to the `position_user` table and put unique index on all the fields, or instead of having `position_user` table add a `position_id` to the `company_user` and do the same. Does that sound good? – Pawel Bieszczad May 07 '15 at 04:02
  • Hi @PawelBieszczad, thanks for the reply. That does sound like a possible option and I will explore it further! – Mhluzi Bhaka May 07 '15 at 06:00
  • I am trying to get this concept working but am struggling. I have added in a company_id to the position_user table but can't see how to update that using "sync" when the company does not yet exist. Any suggestions? – Mhluzi Bhaka May 12 '15 at 02:14
  • If the company does not exist yet, then how could the user have a position there? You need to first create the company, then create the relationship. – Pawel Bieszczad May 12 '15 at 02:41
  • Thanks...I understand your logic point - that is what I am struggling to get right. Added in some changes to the original question. – Mhluzi Bhaka May 12 '15 at 07:15
  • Perhaps my comment above should read: " I am trying to get this concept working but am struggling. I have added in a company_id to the position_user table but can't see how to INSERT that using "sync" when the company does not yet exist. " – Mhluzi Bhaka May 12 '15 at 23:07
  • How can you insert an id of something that doesnt exist yet? It does not have an index assigned to it. You have to first save it to the database. – Pawel Bieszczad May 12 '15 at 23:09
  • Looking at your code further, the company must exist in the database in order for you to retrieve it and create the select input. Perhaps adding the full code of the controller, models and the view will help us understand your problem. – Pawel Bieszczad May 12 '15 at 23:13
  • Thanks for the help so far.. Would like to close this out as I am going to rebuild from scratch as I think there is something flawed in my code concept. Will open up another question if needed. – Mhluzi Bhaka May 13 '15 at 08:25
  • @Pawel, can I mark you as having steered me in the right direction? – Mhluzi Bhaka May 13 '15 at 08:26

2 Answers2

0

I would have three tables: users, companies, and work_history. The work_history would be your pivot table but with a few extra columns: job_title, start_date and end_date. That way, a user can have and belong to many work roles.

If you want roles to be re-used, then you could replace the job_title column in the work_history table with a position_id column.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
0

I asked another question - Laravel 5: synching an extra field via pivot (answered by @lukasgeiter) - and that helped me solve my issue so I would like to have this question marked as closed.

Community
  • 1
  • 1
Mhluzi Bhaka
  • 1,364
  • 3
  • 19
  • 42