Laravel 5.1-3个表之间的数据透视表 [英] Laravel 5.1 - pivot table between 3 tables

查看:62
本文介绍了Laravel 5.1-3个表之间的数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手,在过去的三个星期中一直在实习中进行这项工作,但是现在我遇到了问题.

我有3个表:Users,Bids和Jobs.我希望用户能够对工作进行出价.

我的表的基本结构如下:

Users:
id | name | email | description

Jobs:
id | title | description

Bids:
id | proposal

需要满足以下条件:

  1. 用户可以对很多工作出价,但是他们只能对每个工作出价一次.
  2. 一项工作可以有来自许多用户的许多出价.
  3. 每个出价仅链接到一个用户和一个工作.

示例: 用户1对工作1提出300英镑的投标.这将创建ID为1的投标和300英镑的投标.数据透视表将包含用户ID(1),工作ID(1)和出价ID(1)以及状态(默认情况下将设置为待处理").

我还希望链接所有3个表的状态.我创建了一个如下:

bid_job_user:

bid_id | user_id | job_id | status

但是关系都错了,因此如果我想更新状态,同步方法等将无法正常工作. 感谢您提供任何帮助,即使这正是我应该如何定义自己的人际关系.

我无法将用户链接到表(job_user)中的作业,因为在其他地方已经定义了该表,因为管理者(用户)可以创建许多作业,并且许多管理者可以创建作业,因此存在多对多关系. >

如果您需要更多信息,请询问.

解决方案

要创建出价表(根据评论),可以将其视为交集实体.

<!-- 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');
}

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.

解决方案

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');
}

这篇关于Laravel 5.1-3个表之间的数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆