【问题标题】:Eloquent (kind of) triple relationship雄辩的(某种)三重关系
【发布时间】:2015-04-15 02:04:42
【问题描述】:

我很难找到一个简单的解决方案,使用 Eloquent 关系,我有 3 个模型:

  • 用户
  • 项目
  • 类别

现在一个用户有很多类别,一个类别只有一个用户。一个用户有很多项目,一个项目有多少用户。最后一个项目有很多类别,很多类别有很多项目。棘手的部分是,虽然一个项目可以有很多类别,但每个用户只能有一个类别,因此反过来用户只能将他拥有的一个类别与该项目相关联。

item 和 user(item_user) 之间的数据透视表也有两个属性,boolean is_ownertinyInt 权限

在我的应用程序中,我始终拥有用户 ID

我想检索属于用户的费用,实现如下:

[
    {
        "id": "1",
        "value": "0.40",
        "description": "Expense 0",
        "category": {
            "id": "1",
            "name": "Health",
            "created_at": {
                "date": "2015-04-15 01:33:05",
                "timezone_type": 3,
                "timezone": "UTC"
            },
            "update_at": {
                "date": "2015-04-15 01:33:05",
                "timezone_type": 3,
                "timezone": "UTC"
            }
        },
        "users": [
            {
                "id": "1",
                "name": "Fabio",
                "email": "fabioantuness@gmail.com",
                "permissions": {
                    "expense_id": "1",
                    "user_id": "1",
                    "is_owner": "1",
                    "permissions": "6"
                }
            }
        ]
    },
    {
        "id": "2",
        "value": "12.40",
        "description": "Expense 1",
        "category": {
            "id": "1",
            "name": "Health",
            "created_at": {
                "date": "2015-04-15 01:33:05",
                "timezone_type": 3,
                "timezone": "UTC"
            },
            "update_at": {
                "date": "2015-04-15 01:33:05",
                "timezone_type": 3,
                "timezone": "UTC"
            }
        },
        "users": [
            {
                "id": "1",
                "name": "Fabio",
                "email": "fabioantuness@gmail.com",
                "permissions": {
                    "expense_id": "2",
                    "user_id": "1",
                    "is_owner": "1",
                    "permissions": "6"
                }
            }
        ]
    }
]

物品型号:

class Item extends Model {

    protected $fillable = ['category_id', 'value', 'description'];

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

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

}

用户模型:

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    protected $table = 'users';
    protected $fillable = ['name', 'email', 'password'];
    protected $hidden = ['password', 'remember_token'];

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

    public function items()
    {
        return $this->belongsToMany('App\Item')->withPivot('is_owner', 'permissions');
    }

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

}

类别模型:

class Category extends Model {

    protected $fillable = ['name'];

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

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

【问题讨论】:

    标签: laravel eloquent laravel-5


    【解决方案1】:

    在 stackoverflow 上对问题进行图解帮助我找到了解决方案,这是查询:

    User::expenses()->with(
        [
            'users',
            'categories' => function ($query) use ($userId)
            {
                $query->where('user_id', $userId);
            }
        ]
    )->get()->all();
    

    如果有人有更好的解决方案,请随时分享

    【讨论】:

    • 我打赌你是“问橡皮鸭”的粉丝:blog.codinghorror.com/rubber-duck-problem-solving 很高兴你得到答案!
    • 现在我来了!我正在考虑买一个便携式的,这样我可以在家里和工作中问他。感谢您的文章。
    • 哈哈没问题 :) 老实说,这种方法对我帮助很大。当我在这里输入问题时,我发现自己在回答自己的问题。
    猜你喜欢
    • 1970-01-01
    • 2017-10-16
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2018-03-28
    相关资源
    最近更新 更多