json获取平均值

时间:2018-06-26 08:39:32

标签: php json laravel

我有一个显示“ tarificationtaches”列表的函数,我想添加一个平均值以从表avis_intervention中获取平均“ techniciens” moyenne_avis,其中“ intervention.technicien_id” =“ tarificationtaches.techncien_id”,在我的模式中。

tarificationtaches_tables

Schema::create('tarificationtaches', function (Blueprint $table) {
        $table->increments('id');
        $table->float('tarif', 8,2);
        $table->integer('tache_id')->unsigned();
        $table->foreign('tache_id')->references('id')->on('taches');
        $table->integer('technicien_id')->unsigned();
        $table->foreign('technicien_id')->references('id')- 
        >on('techniciens');
        $table->datetime('deleted_at')->nullable();
        $table->timestamps();
    });

techniciens_tables

 Schema::create('techniciens', function (Blueprint $table) {
        $table->increments('id');
        $table->boolean('actif')->default(1);
        $table->float('moyenne_avis')->nullable();
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->datetime('deleted_at')->nullable();
        $table->timestamps();

    });

avis_interventions_tables

Schema::create('avis_interventions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('qualité');
        $table->integer('nbr_heure');
        $table->string('service');
        $table->float('note', 1,1);
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients');
        $table->integer('intervention_id')->unsigned();
        $table->foreign('intervention_id')->references('id')- 
        >on('interventions');
        $table->timestamps();
    });

interventions_tables

 Schema::create('interventions', function (Blueprint $table) {
        $table->increments('id');
        $table->date('date_intervention')->nullable();
        $table->string('description');
        $table->dateTime('duree_prevu');
        $table->boolean('statut');
        $table->integer('technicien_id')->unsigned();
        $table->foreign('technicien_id')->references('id')- 
        >on('techniciens');
        $table->integer('tarification_id')->unsigned();
        $table->foreign('tarification_id')->references('id')- 
        >on('tarificationtaches');
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('Clients');


        $table->timestamps();
    });

这是我的功能

public function getTar(){
     $tarifications = tarificationtache::with('technicien')->get();

        return $tarifications->map(function ($tarification) {
            return [
                'nom' => $tarification->technicien->user->nom,
                'moyenne_avis' => $tarification->technicien->moyenne_avis,
                'tache' => $tarification->tache->libelle_tache,
                'tarif' => $tarification->tarif,
            ];
        });    



}

它显示如下

[{"nom":"tech 1","moyenne_avis":null,"tache":"tache 2","tarif":29.55}, 
{"nom":"tech     
    2","moyenne_avis":null,"tache":"tache 3","tarif":55.12},{"nom":"tech 
    1","moyenne_avis":null,"tache":"tache 3","tarif":253},{"nom":"tech 
    2","moyenne_avis":null,"tache":"tache 3","tarif":28.22}]

注意:moyenne_avis为空,因为他显示了来自technicien的moyenne_avis,而我没有任何插入。

0 个答案:

没有答案