I would like to pull the record subscription detail record and the plans and cheats that the particular user subscribes. Aside from that, I also included all my relationship that is available in my model(s).
User Model
Columns: id, username, email
public function plans(){
return $this->belongsToMany(Plan::class,"subscriptions","user_id","plan_id");
}
Subscription Model
Columns: id, plan_id, user_id, expiry,dll_type
Plan Model
Columns: id, cheat_id, currency_code, country,amount
public function cheatinfo($val){
return $this->hasOne(Cheat::class,'cheat_id','id');
}
Cheats Model
Columns: id, internal_subscription_name, cheat_name, isActive
public function plans(){
return $this->hasMany(Plan::class)->select(['id','cheat_id','country','currency_code','amount']);
}
This is my attempt and this is my current function caller, by the way, I am new for Laravel Eloquent at the moment.
User::whereId(2)->with(["plans"])->first();
Current output:
{
"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
}
}
]
}
}
Expected output:
{
"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
}
}]
}
}
}
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3oGMimA
via IFTTT
Aucun commentaire:
Enregistrer un commentaire