2017-12-18 9 views
3

私はAPIレスポンスを作成します。このようなトランスフォーマを使用してレスポンスを返します。ピボットフィールドから実際のテーブルに値をコピーする方法Laravel(画像をより詳しく説明します)

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ブランチに焦点を当ててみましょう、私はピボットから値をコピーしたいの応答であります親ブランチにブランチ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'); 
    } 
} 
+0

ですので、量をサービングにコピーする必要がありますか? –

答えて

3

サービングの値をフェッチするたびに、ミューテータを使用して値を変更できます。

Foodモデルに入れてください。

public function getServingsAttribute() 
{ 
    return $this->pivot->quantity; 
} 
+0

答えてくれてありがとう、私も答えを見つけた、あなたがそこに置いたまったく同じように見える:D –

+1

@ MiftahMizwar haha​​。幸せなコーディング! –

関連する問題