How to copy value from pivot field to actual table Laravel (image will more explained)

时间:2017-12-18 07:54:25

标签: laravel eloquent mode

I create an API response, I return the response using transformer like this.

<link rel="stylesheet" type="text/css" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<div class="row">
  <div class="col-md-4 margin-left-26">
    <span class="glyphicon glyphicon-info-sign
    spark-error-info pad-left-10">
      <span class="icon-tooltip" tooltip="Hello this is my fiddle"></span>
    </span>
  </div>
</div>

enter image description here

And here is the response of API

enter image description here

 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,
        ];
    }

}

Let's focus to { "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 } branch, so i want to copy value from pivot branch to parent branch called foods.pivot.

Here is the relation, focus on foods function.

servings

1 个答案:

答案 0 :(得分:3)

You can use a mutator to modify the value of servings whenever it is fetched.

Put this in your {'key': 'key4', 'value': 'value4'}, {'key': 'key2', 'value': 'value2'}, {'key': 'key3', 'value': 'value3'}, {'key': 'key1', 'value': 'value1'} Model.

Food