【问题标题】:Laravel 5.3 : 2 same relationships for 2 same TablesLaravel 5.3:2个相同表的2个相同关系
【发布时间】:2016-12-24 05:48:19
【问题描述】:

我需要帮助来做点什么:我有 2 张桌子:

- Users
----------
id_user


 - Sports
----------
id_sport

title_sport

用户可以练习一项或多项运动。

用户可以搜索从事一项或多项运动的用户。

你能帮我为模型用户吗?

【问题讨论】:

  • 它看起来像 many-to-many。请阅读文档以获取更多信息laravel.com/docs/5.3/eloquent-relationships#many-to-many
  • 感谢您的评论,我同意多对多关系,但我的模型中可以有 2 个相同的关系?
  • 您可以在模型中使用尽可能多的关系。但问题是你为什么需要它以及它的用例是什么。

标签: php eloquent laravel-5.3


【解决方案1】:

我可以使用这样的东西吗?

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{

    public function pratice()
    {
        return $this->belongsToMany('App\Sport');
    }
   public function search_user()
    {
        return $this->belongsToMany('App\Sport');
    }
}

带有 2 个数据透视表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2013-09-01
    • 2021-08-06
    相关资源
    最近更新 更多