2

I'm relatively new to Laravel and have been working on it in my internship for the last 3 weeks however now I have a problem.

I have 3 tables: Users, Bids, and Jobs. I want a user to be able to place a bid on the job.

The basic structure of my tables is as follows:

Users:
id | name | email | description

Jobs:
id | title | description

Bids:
id | proposal

The following criteria need to be met:

  1. A user can bid on many jobs, however they can only bid on each job once.
  2. A job can have many bids from many users.
  3. Each bid is only linked to one user and one job.

Example: User 1 makes a proposal of £300 on Job 1. This creates a bid with id 1 and proposal £300. The pivot table will contain the user id(1), the job id(1) and the bid id(1) along with the status which by default will be set to Pending.

I also want the table that links all 3 to have a status. I created one as follows:

bid_job_user:

bid_id | user_id | job_id | status

But the relationships were all wrong so syncing methods etc wouldn't work correctly if I wanted to update a status. Any help is appreciated, even if it is just how I should define my relationships.

I cannot link the user to the job in a table(job_user) as this is already defined elsewhere as a many to many relationship as managers(users) can create many jobs and jobs can be created by many managers.

If you need any more information please ask.

PHPhil
  • 1,555
  • 13
  • 27
caseylouisee
  • 103
  • 1
  • 12
  • wouldn't it be `User -< bid >- Job` you stated there is already an existing connection between the users and the jobs (the users that created them), so the only connection remaining would be the bids from other users? – AbstractChaos Jul 15 '15 at 14:58
  • @AbstractChaos Only certain users can create jobs which is what the job_user table represents. I want the user to bid on a job so a new table would be needed wouldn't it? how would you create the tables if it was you? – caseylouisee Jul 15 '15 at 15:04
  • @caselouisee Yes exactly you already have a job_user table instructing who can create jobs so you don't need to redefine that. To me it sounds like you just need a bid table (id, user_id, job_id, status, proposal) to connect the Users (the bidders in this case) to the jobs. – AbstractChaos Jul 15 '15 at 15:09
  • @AbstractChaos How would i define the relationships if I did it that way? i.e. public function user(){ return $this->belongsTo('App\User', 'bids'); } public function job(){ return $this->belongsTo('App\Job','bids'); } – caseylouisee Jul 15 '15 at 15:20

1 Answers1

4

To create the bid table (As per comments) you could treat it as a intersection entity.

<!-- User.php -->
//should recognize user_id,job_id by itself
public function bidsMade() {
  return $this->belongsToMany('App\Job','bids')->withPivot('status','proposal');
}

<!-- Job.php -->
//should recognize user_id,job_id by itself
public function bidsRecieved() {
  return $this->belongsToMany('App\User','bids')->withPivot('status','proposal');
}
AbstractChaos
  • 4,211
  • 1
  • 19
  • 28
  • 1
    Thank you! this works, just need the bid model relationships now but I'm happy to work on that myself. Can't upvote you until I have 15 rep -.- but thank you so much!! – caseylouisee Jul 15 '15 at 15:36
  • AbstractChaos, caseylpuisee, I need same problem, please you can show the code final? – Alexd2 Aug 18 '15 at 13:39