json显示为'nom'和avg'note'而不是technical_id

时间:2018-06-21 08:55:07

标签: php json laravel

我有一个Web服务功能来显示带有technicien ID的“ tarificationtache”列表,但我要显示他的名字和“ avis_interventions”的平均值

我有5张桌子

techniciens(id, user_id,actif) 
users (nom , prenom , adresse )
tarificationtaches (id , tarif , technicien_id,tache_id , tarif) 
intervention(id , tarification_id , technicien_id....)
avis_interventions( id , intervention_id , note )

这是我的服务:

public function getTar($id) 
{
    $taches = Tache::with("tarificationtache")->get();
    return response(compact('taches'));
}

他像这样显示:

{"taches":
[
    {
        "id":1,
        "libelle_tache":"montage porte simple",
        "Tarif":null,
        "metier_id":1,
        "deleted_at":null,
        "created_at":"2018-06-08 11:41:41",
        "updated_at":"2018-06-08 11:41:41",
        "tarificationtache":[]
    },
    {
        "id":2,
        "libelle_tache":"tache 1",
        "Tarif":null,
        "metier_id":1,
        "deleted_at":null,
        "created_at":"2018-06-08 11:41:55",
        "updated_at":"2018-06-08 11:41:55",
        "tarificationtache":[]
    },
    {
        "id":3,
        "libelle_tache":"tache 2",
        "Tarif":null,
        "metier_id":2,
        "deleted_at":null,
        "created_at":"2018-06-08 11:42:01",
        "updated_at":"2018-06-08 11:42:01",
        "tarificationtache": [
            {
                "id":1,
                "tarif":29.55,
                "tache_id":3,
                "technicien_id":1,
                "deleted_at":null,
                "created_at":"2018-06-08 11:58:54",
                "updated_at":"2018-06-08 11:58:54"
            }
        ]
    },
    {
        "id":4,
        "libelle_tache":"tache 3",
        "Tarif":null,
        "metier_id":3,
        "deleted_at":null,
        "created_at":"2018-06-08 12:00:15",
        "updated_at":"2018-06-08 12:00:15",
        "tarificationtache": [
            {
                "id":2,
                "tarif":55.12,
                "tache_id":4,
                "technicien_id":2,
                "deleted_at":null, 
                "created_at":"2018-06-08 12:00:37",
                "updated_at":"2018-06-08 12:00:37"
            },
            {
                "id":3,
                "tarif":253,
                "tache_id":4,
                "technicien_id":1,
                "deleted_at":null, 
                "created_at" :"2018-06-15 23:17:23",
                "updated_at":"2018-06-15 23:17:23"
            },
            {
                "id":4,
                "tarif":28.22,
                "tache_id":4,
                "technicien_id":2,
                "deleted_at":null, 
                "created_at":"2018-06-20 17:03:32",
                "updated_at":"2018-06-20 17:03:32"
            }
        ]
    }
]

}

说我使用此功能在laravel中获得了平均评分

public function show($id)
{
    $technicien = technicien::findOrFail($id);
    $notes = $technicien->avisinterventions->pluck('note');
    $moyenne = $technicien->avisinterventions()->avg('note');


    $user = $technicien->user;
    $metier = $technicien->metier;
    $tarificationtache = $technicien->tarificationtache;
    $zoneintervention = $technicien->zoneintervention;


    return view('technicien.show' , compact('technicien'))
        ->with('user',$user)
        ->with('metier',$metier)
        ->with('zoneintervention',$zoneintervention)
        _>with('tarificationtache',$tarificationtache)
        ->with('moyenne',$moyenne);
}

0 个答案:

没有答案