如何将值从pivot字段复制到实际表Laravel(图像将更加解释)

问题描述 投票:3回答:1

我创建了一个API响应,我使用这样的变换器返回响应。

 public function transform(DietPlan $dietPlan)
    {   
            // $dietPlan->foods->diet_plan_id = $dietPlan->id;

        return [
                        'id'          => $dietPlan->id,
                        'bmi_status'  => $dietPlan->bmi_status,
                        'eating_type' => $dietPlan->eating_type,
                        'eating_time' => $dietPlan->eating_time,
                        'disease'     => ($dietPlan->diseases_id == null) ? null : $dietPlan->diseases->name,
                        'blood_group' => ($dietPlan->blood_groups_id == null) ? null : $dietPlan->blood_group->name,
                        'age'         => $dietPlan->age,
                        'foods'       => $dietPlan->foods,
                        'created_at'  => $dietPlan->created_at,
                        'updated_at'  => $dietPlan->updated_at,
        ];
    }

enter image description here

以下是API的响应

enter image description here

{
"status": "0",
"message": "success",
"response": {
    "headers": {},
    "original": {
        "data": [
            {
                "id": 35,
                "bmi_status": "Normal Weight",
                "eating_type": "Breakfast",
                "eating_time": "02:55:00",
                "disease": "Diabetes",
                "blood_group": null,
                "age": 20,
                "foods": [
                    {
                        "id": 2,
                        "food_name": "Potato",
                        "protein": "10",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "80",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-11-04 08:36:31",
                        "image": null,
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 35,
                            "food_id": 2,
                            "quantity": "4"
                        }
                    },
                    {
                        "id": 3,
                        "food_name": "Rise",
                        "protein": "40",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "100",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-11-04 08:36:31",
                        "image": null,
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 35,
                            "food_id": 3,
                            "quantity": "4"
                        }
                    },
                    {
                        "id": 4,
                        "food_name": "Salad",
                        "protein": "30",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "15",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-12-15 19:42:05",
                        "image": "http://bmiproject.app/images/foods/saladvuy8tb-4.jpg",
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 35,
                            "food_id": 4,
                            "quantity": "5"
                        }
                    },
                    {
                        "id": 18,
                        "food_name": "Balabala",
                        "protein": "2",
                        "calories": null,
                        "fact": "Balabalba",
                        "carbohydrate": "1",
                        "unit_type": null,
                        "created_at": "2017-12-15 19:29:02",
                        "updated_at": "2017-12-15 19:29:02",
                        "image": null,
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 35,
                            "food_id": 18,
                            "quantity": "7"
                        }
                    }
                ],
                "created_at": {
                    "date": "2017-12-16 06:54:11.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                },
                "updated_at": {
                    "date": "2017-12-16 06:54:11.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                }
            },
            {
                "id": 36,
                "bmi_status": "Normal Weight",
                "eating_type": "Lunch",
                "eating_time": "02:55:00",
                "disease": "Diabetes",
                "blood_group": null,
                "age": 20,
                "foods": [
                    {
                        "id": 1,
                        "food_name": "Meat",
                        "protein": "27",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "35",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-11-04 08:36:31",
                        "image": null,
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 36,
                            "food_id": 1,
                            "quantity": "2"
                        }
                    },
                    {
                        "id": 3,
                        "food_name": "Rise",
                        "protein": "40",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "100",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-11-04 08:36:31",
                        "image": null,
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 36,
                            "food_id": 3,
                            "quantity": "2"
                        }
                    },
                    {
                        "id": 15,
                        "food_name": "Bacang",
                        "protein": "3",
                        "calories": null,
                        "fact": "Hello Bacang adalah",
                        "carbohydrate": "3",
                        "unit_type": null,
                        "created_at": "2017-12-15 13:10:07",
                        "updated_at": "2017-12-15 13:10:07",
                        "image": "http://bmiproject.app/images/foods/bacangfeschp-4.jpg",
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 36,
                            "food_id": 15,
                            "quantity": "2"
                        }
                    },
                    {
                        "id": 15,
                        "food_name": "Bacang",
                        "protein": "3",
                        "calories": null,
                        "fact": "Hello Bacang adalah",
                        "carbohydrate": "3",
                        "unit_type": null,
                        "created_at": "2017-12-15 13:10:07",
                        "updated_at": "2017-12-15 13:10:07",
                        "image": "http://bmiproject.app/images/foods/bacangfeschp-4.jpg",
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 36,
                            "food_id": 15,
                            "quantity": "2"
                        }
                    },
                    {
                        "id": 15,
                        "food_name": "Bacang",
                        "protein": "3",
                        "calories": null,
                        "fact": "Hello Bacang adalah",
                        "carbohydrate": "3",
                        "unit_type": null,
                        "created_at": "2017-12-15 13:10:07",
                        "updated_at": "2017-12-15 13:10:07",
                        "image": "http://bmiproject.app/images/foods/bacangfeschp-4.jpg",
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 36,
                            "food_id": 15,
                            "quantity": "2"
                        }
                    }
                ],
                "created_at": {
                    "date": "2017-12-16 06:54:38.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                },
                "updated_at": {
                    "date": "2017-12-16 06:54:38.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                }
            },
            {
                "id": 37,
                "bmi_status": "Normal Weight",
                "eating_type": "Evening Snack",
                "eating_time": "02:56:00",
                "disease": "Diabetes",
                "blood_group": null,
                "age": 20,
                "foods": [
                    {
                        "id": 2,
                        "food_name": "Potato",
                        "protein": "10",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "80",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-11-04 08:36:31",
                        "image": null,
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 37,
                            "food_id": 2,
                            "quantity": "9"
                        }
                    },
                    {
                        "id": 1,
                        "food_name": "Meat",
                        "protein": "27",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "35",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-11-04 08:36:31",
                        "image": null,
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 37,
                            "food_id": 1,
                            "quantity": "1"
                        }
                    }
                ],
                "created_at": {
                    "date": "2017-12-16 06:55:09.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                },
                "updated_at": {
                    "date": "2017-12-16 06:55:09.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                }
            },
            {
                "id": 38,
                "bmi_status": "Normal Weight",
                "eating_type": "Dinner",
                "eating_time": "02:56:00",
                "disease": "Diabetes",
                "blood_group": null,
                "age": 20,
                "foods": [
                    {
                        "id": 1,
                        "food_name": "Meat",
                        "protein": "27",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "35",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-11-04 08:36:31",
                        "image": null,
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 38,
                            "food_id": 1,
                            "quantity": null
                        }
                    },
                    {
                        "id": 4,
                        "food_name": "Salad",
                        "protein": "30",
                        "calories": null,
                        "fact": "Fact Dummy",
                        "carbohydrate": "15",
                        "unit_type": null,
                        "created_at": "2017-11-04 08:36:31",
                        "updated_at": "2017-12-15 19:42:05",
                        "image": "http://bmiproject.app/images/foods/saladvuy8tb-4.jpg",
                        "servings": null,
                        "pivot": {
                            "diet_plan_id": 38,
                            "food_id": 4,
                            "quantity": null
                        }
                    }
                ],
                "created_at": {
                    "date": "2017-12-16 06:55:28.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                },
                "updated_at": {
                    "date": "2017-12-16 06:55:28.000000",
                    "timezone_type": 3,
                    "timezone": "UTC"
                }
            }
        ]
    },
    "exception": null
}

}

让我们关注foods.pivot分支,所以我想将值从pivot分支复制到名为servings的父分支。

这是关系,关注食物功能。

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class DietPlan extends Model
{
    protected $fillable = [
        'bmi_status', 'eating_type', 'eating_time', 'diseases_id', 'blood_group_id', 'age'
    ];

    public function eating_time_human()
    {
         return \Carbon\Carbon::createFromFormat('g:i A', $this->eating_time);
    } 

    public function foods()
    {
        return $this->belongsToMany('App\Food', 'diet_plan_foods')->withPivot('quantity');
    }

    public function diseases()
    {
        return $this->belongsTo('App\Diseases', 'diseases_id');
    }

    public function blood_group()
    {
        return $this->hasOne('App\BloodGroup', 'id', 'blood_group_id');
    }
}
laravel eloquent mode
1个回答
3
投票

无论何时获取服务,您都可以使用mutator修改服务的值。

把它放在你的Food模型中。

public function getServingsAttribute()
{
    return $this->pivot->quantity;
}
© www.soinside.com 2019 - 2024. All rights reserved.