雄辩(有点)三重关系

时间:2015-04-15 02:04:43

标签: laravel eloquent laravel-5

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

  • 用户
  • 项目
  • 分类

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

项目和用户之间的数据透视表(item_user)有两个属性,布尔is_owner tinyInt权限

在我的申请期间,我总是拥有用户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');
    }
}

1 个答案:

答案 0 :(得分:0)

在stackoverflow上对这里的问题进行模式化帮助我找到解决方案,这里是查询:

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

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