- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我们取 Laravels example在 hasManyThrough
上并将其更改如下:
countries
id - integer
name - string
users
id - integer
country_id - integer
website_id - integer
name - string
websites
id - integer
title - string
以users
作为中间关系,我可以在country
模型上使用hasManyThrough()
来获取websites
?
我尝试了这个,结果出现错误
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
/**
* Get all of the websites for the country.
*/
public function websites()
{
return $this->hasManyThrough('App\Website', 'App\User');
}
}
Illuminate\Database\QueryException:SQLSTATE[42S22]:未找到列:1054 未知列
也许用户
必须拥有许多网站
才能实现此功能?
最佳答案
您可以根据 @Samir 的评论更改表结构以匹配 Laravel 的期望,也可以自定义键以匹配现有的表结构。
https://laravel.com/docs/5.8/eloquent-relationships
Typical Eloquent foreign key conventions will be used when performing the relationship's queries. If you would like to customize the keys of the relationship, you may pass them as the third and fourth arguments to the hasManyThrough method. The third argument is the name of the foreign key on the intermediate model. The fourth argument is the name of the foreign key on the final model. The fifth argument is the local key, while the sixth argument is the local key of the intermediate model:
public function websites()
{
return $this->hasManyThrough('App\Website', 'App\User','country_id','id','website_id','website_id');
}
关于mysql - 在目标表具有一对一的情况下使用 hasManyThrough,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57448253/
描述: 我的应用程序结构如下: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 | ---------------------
我是一名优秀的程序员,十分优秀!