- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我浪费了一整天的时间,但无法弄清楚 hasManyThrough 关系。
我有这样的关系:
# get related users
public function users()
{
return $this->hasManyThrough(User::class, FuneralHomeUser::class, 'user_id', 'id');
}
这会生成这个查询:
SELECT `users`.*, `funeral_homes_users`.`user_id`
FROM `users`
INNER JOIN `funeral_homes_users` ON `funeral_homes_users`.`id` = `users`.`id`
WHERE `funeral_homes_users`.`user_id` = '4'
一切都很好,除了 ON funeral_homes_users.id = users.id
应该是 ON funeral_homes_users.user_id = users.id
。所以我唯一想得到的是 id
,它应该有 user_id
用于 funeral_homes_users.id
(例如它应该是 funeral_homes_users.user_id
) 但我无法弄清楚。
以下是供引用的表格:
// funeral_homes
Schema::create('funeral_homes', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 255);
$table->timestamps();
});
// funeral_homes_users
Schema::create('funeral_homes_users', function (Blueprint $table) {
$table->increments('id');
$table->integer('funeral_home_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
});
// users
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->string('first_name');
$table->string('last_name');
$table->timestamps();
});
如有任何帮助,我们将不胜感激。谢谢!!!
最佳答案
如果我没理解错的话,你的情况是:
USER
有很多 FUNERAL_HOME
FUNERAL_HOME
属于许多 USER
如果那是正确的,你的关系方法应该返回这样的东西:
User.php
public function FuneralHomes()
{
return $this->belongsToMany(FuneralHome::class, 'funeral_homes_users');
}
FuneralHome.php
public function Users()
{
return $this->belongsToMany(User::class, 'funeral_homes_users');
}
看看doku
关于php - Laravel: hasManyThrough 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401997/
描述: 我的应用程序结构如下:Property与 Manager 有多对多关系, 和 Unit与 Property 有一对多关系,即一个经理可以管理多个属性,一个属性可以有多个经理帐户,一个属性可以有
我有这张 table , categories: id | name | slug Products: id | name | slug | category_id | brand_id Brands
我正在为一个项目开发小型 CMS,其表格如下: - pages - id … - translations - page_id … - menus - id
我想获取帖子的标签。 这是我的数据透视表 post body tag name post_tag post_id tag_id 据我所知并能够理解hasManyThrough() 是为
在我的 Laravel 应用程序中,我有以下类: class Product extends Model { public function extended() {
如果我们取 Laravels example在 hasManyThrough 上并将其更改如下: countries id - integer name - string users
我正在使用 laravel 创建一个消息传递应用程序。我使用了四种迁移(用户、对话、对话成员和对话回复)。我还定义了模型之间的关系。 用户的详细信息将存储在用户表中。对话 ID 将存储在对话表中。每个
我有以下型号: Product BaseProduct BaseCode Series 它们中的每一个都是一个独立的实体,但它们是相关的。 Product有一个指向 BaseProduct 的外键,
这些 HasManyThrough 与 HasAndBelongsToMany 之间有什么区别?? https://docs.strongloop.com/display/public/LB/HasM
我正在尝试获取属于custom_view 的所有过滤器。例如:$user->custom_views->first->filters 下面的代码给了我一个空集合。我已经尝试了所有方法,但无法理解为什么
我需要有关 Laravel Eloquent 模型关系的帮助。我有三张 table : 项目 item_id slideshowID 幻灯片 slideshow_id 图像 image_id slid
我浪费了一整天的时间,但无法弄清楚 hasManyThrough 关系。 我有这样的关系: # get related users public function users() { retu
我的用户模型中有以下关系 public function events($user_id) { return $this->find($user_id)->hasManyThrough('Ev
Laravel 文档似乎表明 hasManyThrough 声明只能用于“深”两级的关系。更复杂的关系呢?例如,一个User有很多Subject,每个Subject有很多Deck,每个Deck有很多C
我有 3 个表:company users invoice。 公司 hasMany 用户。 一个用户 belongsTo 一个公司并且一个用户 hasMany 发票。 一张发票属于一个用户。 现在
您好,我的实体之间存在以下关系。 User - id - other stuff NeighborhoodFilter - id - userId - neighborhoodId
在 Eloquent 的文档中,据说我可以将所需关系的键传递给 hasManyThrough . 假设我有名为 Country、User、Post 的模型。一个 Country 模型可能有许多 Pos
table groups ( id int auto_increment primary key, ... ); table users ( id int auto_incre
我有这些表: 条目表 --------------------------------- | id | blog_id | title | ---------------------
我有这些表: 条目表 --------------------------------- | id | blog_id | title | ---------------------
我是一名优秀的程序员,十分优秀!