【问题标题】:Laravel Eloquent use nested relationship to construct eloquent collectionLaravel Eloquent 使用嵌套关系构建 eloquent 集合
【发布时间】:2021-08-11 23:33:13
【问题描述】:

我想拉记录订阅明细记录和特定用户订阅的计划和秘籍。除此之外,我还包括了我的模型中可用的所有关系。

用户模型

// Columns: id, username, email

public function plans(){
    return $this->belongsToMany(Plan::class,"subscriptions","user_id","plan_id");
}

订阅模式

// Columns: id, plan_id, user_id, expiry,dll_type

计划模型

// Columns: id, cheat_id, currency_code, country,amount

public function cheatinfo($val){
    return $this->hasOne(Cheat::class,'cheat_id','id');
}

作弊模型

// Columns: id, internal_subscription_name, cheat_name, isActive

public function plans(){
    return $this->hasMany(Plan::class)->select(['id','cheat_id','country','currency_code','amount']);
}

这是我的尝试,这是我当前的函数调用者,顺便说一下,我现在是 Laravel Eloquent 的新手。

User::whereId(2)->with(["plans"])->first();

电流输出

{
    "resp": {
        "id": 2,
        "name": "qx.wong",
        "email": "user@gmail.com",
        "email_verified_at": null,
        "created_at": "2021-05-23T07:05:13.000000Z",
        "updated_at": "2021-05-23T07:05:13.000000Z",
        "plans": [
            {
                "id": 5,
                "country": "MY",
                "amount": 40,
                "currency_code": "MYR",
                "cheat_id": 3,
                "created_at": "2021-05-23T04:43:04.000000Z",
                "updated_at": "2021-05-23T04:43:04.000000Z",
                "pivot": {
                    "user_id": 2,
                    "plan_id": 5
                }
            }
        ]
    }
}

预期输出

{
    "resp": {
        "id": 2,
        "name": "qx.wong",
        "email": "user@gmail.com",
        "email_verified_at": null,
        "created_at": "2021-05-23T07:05:13.000000Z",
        "updated_at": "2021-05-23T07:05:13.000000Z",
        "subscription": {
            "expiry":"2020-05-04",
            "dll_type":"2021",
            "plans":[
                {
                    "id": 5,
                    "country": "US",
                    "amount": 10,
                    "currency_code": "USD",
                    "cheat_id": 3,
                    "created_at": "2021-05-23T04:43:04.000000Z",
                    "updated_at": "2021-05-23T04:43:04.000000Z",
                    "pivot": {
                        "user_id": 2,
                        "plan_id": 5
                    },
                    "cheats":{
                        "id":3,
                        "internal_subscription_name":"pubg-ultimate-02",
                        "cheats_name":"PUBG Ultimate 2",
                        "isActive":1
                    }
                }
            ]
        }
    }
}

【问题讨论】:

    标签: laravel laravel-5 eloquent eloquent-relationship


    【解决方案1】:

    您可以使用嵌套关系和点运算符来访问,如下所示

    User::whereId(2)->with(["plans","palns.cheatinfo"])->first();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多