2017-12-18 1 views
3

Ich erstelle eine API-Antwort, ich gebe die Antwort mit Transformator wie folgt zurück.So kopieren Sie den Wert vom Pivotfeld in die tatsächliche Tabelle Laravel (Bild wird genauer erklärt)

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

Und hier ist die Antwort von 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 
} 

}

Konzentrieren wir uns auf foods.pivot Zweig, so möchte ich Wert von Pivot kopieren Verzweigen zum übergeordneten Zweig genannt servings.

Hier ist die Beziehung, Fokus auf Lebensmittel-Funktion.

<?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

so müssen Sie die Menge in Portionen kopieren? –

Antwort

3

Sie können einen Mutator verwenden, um den Wert von Portionen zu ändern, wann immer er abgerufen wird. Geben Sie dieses in Ihr Food Modell ein.

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

danke für die antwort, ich habe die antwort zu finden, genau gleich aussehen sie dort platziert: D –

+1

@MiftahMizwar haha. fröhliche Codierung! –

Verwandte Themen