- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个带有数据透视表的表(用户和饮料),用户与配置文件表有 hasOne 关系,有没有办法将配置文件表附加到数据透视表并获取所有数据。
表格用户
id | name | email
表格配置文件
id | user_id | picture
餐 table 饮料
id | name | price
数据透视表user_drinks
id | user_id | drink_id | quantity | price | status
饮料模型
public function users()
{
return $this->belongsToMany('App\User', 'user_drinks', 'drink_id', 'user_id')->withPivot('price', 'quantity')->withTimestamps();
}
用户模型
public function drinks()
{
return $this->belongsToMany('App\Drink', 'user_drinks', 'drink_id', 'user_id')->withPivot('price', 'quantity')->withTimestamps();
}
public function profile() {
return $this->hasOne('App\Profile');
}
PS:我不想用原始 SQL 查询来写这个,它让我发疯。
最佳答案
我不会更改表关系。我会使用->with()
函数获取现有关系中的个人资料信息。
所以$user->drinks()->where('drink_id', $drink_id)->with('profile')->get();
这是我的猜测。
关于php - 将额外模型附加到 BelongstoMany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63021679/
我有两个模型:用户和经济事件。它们通过 UserEconomicActivity 关联(这是一个 BelongsToMany 关系)。在 User 模型中,我定义了这样的关联: this.belong
考虑下表: user id name client id name user_client user_id client_id rate ... 我希望我的 Contr
给定以下 belongsToMany 关系: 标签模型: class Tag extends Model { public function posts (){ return
假设我有以下模型: const Item = sequelize.define("Item", { active: { type: DataTypes.BOOLEAN, defaultValu
我使用了 Sequelize 并使用 belongsToMany() 设置了多对多关系。但是,当我查询出数据时,像这样: api.models.operator.find({ where:
在Laravel中定义多对多关系时,使用belongsToMany()或hasManyThrough()有什么区别? 例:UserAccountAccount_User 因此,用户通过Account_
我有两个带有数据透视表的表(用户和饮料),用户与配置文件表有 hasOne 关系,有没有办法将配置文件表附加到数据透视表并获取所有数据。 表格用户 id | name | email 表格配置文件 i
我有两个带有数据透视表的表(用户和饮料),用户与配置文件表有 hasOne 关系,有没有办法将配置文件表附加到数据透视表并获取所有数据。 表格用户 id | name | email 表格配置文件 i
我有两个表具有 belongsToMany 关系。数据透视表还包含一个名为 state 的列,它可以有 3 个不同的值。假设我的表是 Table1 和 Table2。 具有不同状态的相同Table1-
所以我有一个名为 thread_user 的数据透视表,并且有两个模型; User.php 和 Thread.php。 我的 User.php 文件中有这个: public function thre
我在 PostgreSQL 上使用 Sequelize 来存储属于组织的用户。组织拥有用户可以访问的设备。因此,从本质上讲,用户也通过他们的组织拥有设备。 我将其设置为每个设备都使用 organiza
我正在 Laravel 中实现多对多关系。这些实体是:用户角色和数据透视表 user_role Users ==== id name ..... roles ==== id role ... u
我试图建立多对多的关系,在这个例子中是用户和角色。 在用户模型中我得到了这个: public function roles() { return $this->belongsToMany('A
我有如下三个表: 用户 id|名称|用户名|密码 角色 编号|名称 用户角色 id|user_id|role_id 这些表通过 belongsToMany 进行通信。我想找到一种方法来选择“users
我的项目中有 belongsToMany 关系: 公共(public)函数产品() { 返回 $this->belongsToMany(Product::class,'order_products',
我有以下模型。 class Training extends \Eloquent { // Add your validation rules here public static $
所以我通过数据透视表 user_photo 在 Users 和 Photos 之间建立了多对多关系。我在我的用户模型中使用 belongsToMany('Photo')。然而,这里的问题是我的照片表中
我有这个给定的表结构: 如何使用 Eloquent 从我的“visita”类访问“curso.name”?我分配了多对多关系,但只能访问“turma.curso_id”,但我想得到类似 $visita
使用 sequelize,我期望这一行: m.User.belongsToMany(m.Company, {through: 'UserCompany'}); 在我的数据库中生成一个名为“user_c
我有两个表:categories 和 videos,然后我有一个数据透视表,因为它们是 belongsToMany 关系。 我想要做的是获取所有视频,其中没有一个视频实例属于多个类别之一。 例如 视频
我是一名优秀的程序员,十分优秀!