douyou2234 2015-07-15 14:40
浏览 37
已采纳

Laravel 5.1 - 3个表之间的数据透视表

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.

  • 写回答

1条回答 默认 最新

  • douqiang6036 2015-07-15 15:30
    关注

    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');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题