首页 \ 问答 \ Laravel:使用Eloquent ORM的N到N(mysql)关系(Laravel: N to N (mysql) relation using Eloquent ORM)

Laravel:使用Eloquent ORM的N到N(mysql)关系(Laravel: N to N (mysql) relation using Eloquent ORM)

我有4张桌子:

// Table countries
+----+------+
| Id | Name |
+----+------+
|  1 | USA  |
|  2 | GB   |
+----+------+

// Table platforms
+----+---------+
| Id |  Name   |
+----+---------+
|  1 | Windows |
|  2 | Linux   |
+----+---------+

// Table users
+----+-------+------------+-------------+
| Id | Name  | country_id | platform_id |
+----+-------+------------+-------------+
|  1 | Admin |          1 |           1 |
|  2 | Test  |          2 |           1 |
+----+-------+------------+-------------+

// Table posts
+----+-----------+------------+-------------+---------+
| Id |   Title   | country_id | platform_id | user_id |
+----+-----------+------------+-------------+---------+
|  1 | TestPost1 |          2 |           1 | 1       |
|  2 | TestPost2 |          2 |           2 | null    |
+----+-----------+------------+-------------+---------+

数据库应该能够实现以下关系:

  • 用户(N)< - >(N)平台
  • 用户(N)< - >(N)国家
  • 用户(0..1)< - >(N)发布
  • 邮政(N)< - >(N)国家
  • 发布(N)< - >(1)平台

所以现在我尝试在Laravel Eloquent ORM文档之后实现这些关系:

  // Country.php
  public function posts()
  {
      return $this->belongsToMany('App\Post');
  }

  public function users()
  {
      return $this->belongsToMany('App\User');
  }

  // Platform.php
  public function users()
  {
      return $this->belongsToMany('App\User');
  }

  public function posts()
  {
      return $this->belongsToMany('App\Post');
  }

  // User.php
  public function posts()
    {
        return $this->hasMany('App\Post');
    }

    public function countries()
    {
        return $this->hasMany('App\Country');
    }

    public function platforms()
    {
          return $this->hasMany('App\Platform');
    }

  // Post.php
  public function countries()
  {
      return $this->hasMany('App\Country');
  }

  public function platforms()
  {
      return $this->hasMany('App\Comment');
  }

  public function user()
  {
      return $this->belongsTo('App\User', 'user_id');
  }

但现在我很困惑,因为我认为在mysql中实现N到N关系的方法是向db添加第三个表,例如:

// Table CountryUserRelations to implement User (N) <-> (N) Country
+----+------------+---------+
| Id | country_id | user_id |
+----+------------+---------+
|  1 |          1 |       1 |
|  2 |          2 |       1 |
|  3 |          1 |       2 |
|  4 |          2 |       2 |
+----+------------+---------+

但是Eloquent ORM如何处理我模型中的规则? 它是否会保持N到N的关系而不必添加关系表? 或者我错过了什么或误解了雄辩的ORM关系概念?


I got 4 tables:

// Table countries
+----+------+
| Id | Name |
+----+------+
|  1 | USA  |
|  2 | GB   |
+----+------+

// Table platforms
+----+---------+
| Id |  Name   |
+----+---------+
|  1 | Windows |
|  2 | Linux   |
+----+---------+

// Table users
+----+-------+------------+-------------+
| Id | Name  | country_id | platform_id |
+----+-------+------------+-------------+
|  1 | Admin |          1 |           1 |
|  2 | Test  |          2 |           1 |
+----+-------+------------+-------------+

// Table posts
+----+-----------+------------+-------------+---------+
| Id |   Title   | country_id | platform_id | user_id |
+----+-----------+------------+-------------+---------+
|  1 | TestPost1 |          2 |           1 | 1       |
|  2 | TestPost2 |          2 |           2 | null    |
+----+-----------+------------+-------------+---------+

The database should be able to implement the following relations:

  • User (N) <-> (N) Platform
  • User (N) <-> (N) Country
  • User (0..1) <-> (N) Post
  • Post (N) <-> (N) Country
  • Post (N) <-> (1) Platform

So now I tried to implement these relations following Laravel Eloquent ORM documentation:

  // Country.php
  public function posts()
  {
      return $this->belongsToMany('App\Post');
  }

  public function users()
  {
      return $this->belongsToMany('App\User');
  }

  // Platform.php
  public function users()
  {
      return $this->belongsToMany('App\User');
  }

  public function posts()
  {
      return $this->belongsToMany('App\Post');
  }

  // User.php
  public function posts()
    {
        return $this->hasMany('App\Post');
    }

    public function countries()
    {
        return $this->hasMany('App\Country');
    }

    public function platforms()
    {
          return $this->hasMany('App\Platform');
    }

  // Post.php
  public function countries()
  {
      return $this->hasMany('App\Country');
  }

  public function platforms()
  {
      return $this->hasMany('App\Comment');
  }

  public function user()
  {
      return $this->belongsTo('App\User', 'user_id');
  }

But now I am confused, as I thought the way to implement N to N relations in mysql is to add a third table to db, for example like that:

// Table CountryUserRelations to implement User (N) <-> (N) Country
+----+------------+---------+
| Id | country_id | user_id |
+----+------------+---------+
|  1 |          1 |       1 |
|  2 |          2 |       1 |
|  3 |          1 |       2 |
|  4 |          2 |       2 |
+----+------------+---------+

But how does Eloquent ORM handle the rules inside my model? Will it keep the N to N relations without having to add a relations table? Or am I missing something or misunderstanding the Eloquent ORM Relations concept?


原文:https://stackoverflow.com/questions/43416437
更新时间:2023-10-02 22:10

最满意答案

这是因为你将TintColoralpha设置为0 。 它等于不可见。

什么是按钮上的tintColor

应用于按钮标题和图像的色调颜色。

所以你需要修改第3行的xib,如下所示

<color key="tintColor" red="0.0" green="0.0" blue="0.0" alpha="1.0" colorSpace="custom" customColorSpace="sRGB"/>

It is because of that you set the alpha of TintColor to 0. It equals to invisible.

What is tintColor on Button

The tint color to apply to the button title and image.

So you need to modify the xib at line 3 as below

<color key="tintColor" red="0.0" green="0.0" blue="0.0" alpha="1.0" colorSpace="custom" customColorSpace="sRGB"/>

相关问答

更多
  • 您可以设置按钮的Padding
  • 这是Xamarin.Forms中的默认行为。 要解决该问题并实现自定义颜色,您可以: 使用触发器在从启用到禁用时更改颜色,反之亦然 在启用和禁用按钮时手动设置颜色 使用自定义渲染器 因为触发器是实现你想要的最直接,最一致的方式,所以我只会介绍它。 您可以看到xamarin 文档以获取更多关于自定义渲染器的信息。 使用触发器时,可以在未启用时动态地将字体颜色更改为灰色。 有关触发器的更多信息,请参阅文档 。
  • 嘿Namratha,如果你要求改变UIButton的文本和启用/禁用状态,它可以很容易地完成如下; [myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title [myButton setEnabled:NO]; // To toggle enabled / disabled 如果您已经在Interface Builder中创建了按钮并希望以代码方式访问它们,则可以利用它们作为参数传 ...
  • 这是因为你将TintColor的alpha设置为0 。 它等于不可见。 什么是按钮上的tintColor 应用于按钮标题和图像的色调颜色。 所以你需要修改第3行的xib,如下所示 It is because of that you set the alpha of TintColo ...
  • 这是因为字体不会神奇地使自己大胆。 粗体字体是分开设计的。 恐怕这个问题没有答案,除了增加线条的中风,这可能会违反你的字体协议。 That's because fonts don't magically make themselves bold. Bold fonts are designed separately. I'm afraid there is no answer to this question, apart from thickening the stroke of the lines, w ...
  • 关于什么 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(5, 0) forBarMetrics:UIBarMetricsDefault]; What about [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(5, 0) forBarMetrics:UIBarMetricsDefau ...
  • 后面的标题取决于ViewController的Title属性。 它可以如下设置,这里vc是在UINavigationController内部推入的控制器的实例。 vc.NavigationItem.SetLeftBarButtonItem (new UIBarButtonItem( UIImage.FromFile("29_icon.png"), UIBarButtonItemStyle.Plain, (sender, args) => { NavigationController.PopViewC ...
  • 这是关于约束。 在设备上使其变得更大/更小时,您需要将按钮的边缘与视图的边缘相连:单击Storyboard工作区下方看起来像Tie-Fighter的图标。 要在文本和按钮边缘之间获得间距,请使用内容插入 - 位于尺寸检查器面板上(右侧有一个图标看起来像垂直标尺的那个)。实际上,您应该将自动布局和尺寸约束读为构建UI将需要对此材料有很好的了解。 This is all about Constraints. In terms of making it bigger/smaller on devices you ...
  • 这些是html实体,也适用于android studio。 > can be replaced with > < can be replaced with < " can be replaced with ", “ or ” ' can be replaced with ', ‘ or ’ } can be replaced with } & can be replaced with & These are ...
  • 您将要做的是用文本字段替换标签,并在用户按下按钮时将其设置为第一响应者。 您将需要一个作为文本字段的委托的类,以便您处理输入textFieldShouldReturn方法以在文本字段上重新签名第一个响应者(以关闭键盘)并将文本字段更改回标签视图并更新其内容。 What you are going to have to do is replace the label with a text field and set that as the first responder when the user pres ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的