杰森:如何添加平均值以获得平均值

时间:2018-06-26 09:35:13

标签: json laravel

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

taches_table

public function up()
{
    Schema::create('taches', function (Blueprint $table) {
        $table->increments('id');
        $table->string('libelle_tache');
        $table->float('Tarif', 8,2)->nullable();
        $table->integer('metier_id')->unsigned();
        $table->foreign('metier_id')->references('id')->on('metiers');
        $table->datetime('deleted_at')->nullable();
        $table->timestamps();
    });
}

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

enter image description here

2 个答案:

答案 0 :(得分:1)

编辑:

下面是模型文件,后面是我用来生成输出的模型关系雄辩查询:

技术模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;

class technicien extends Model
{
    protected $table = "techniciens";

    public function user()
    {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }
}

tarificationtache模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\technicien;
use App\tache;
use App\interventions;

class tarificationtache extends Model
{
    protected $table = "tarificationtaches";

    public function technicien()
    {
        return $this->belongsTo('App\technicien', 'technicien_id', 'id');
    }

    public function tache()
    {
        return $this->belongsTo('App\tache', 'tache_id', 'id');
    }

    public function interventions()
    {
        return $this->hasMany('App\interventions', 'technicien_id', 'id');
    }
}

行程模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class tache extends Model
{
   protected $table = "taches";
}

干预模式:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\avis_interventions;

class interventions extends Model
{
   protected $table = "interventions";

   public function avis_interventions()
   {
        return $this->hasMany('App\avis_interventions', 'intervention_id', 'id');
   }
}

avis_interventions模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class avis_interventions extends Model
{
   protected $table = "avis_interventions";
}

模型关系查询:

$tarifications = tarificationtache::with('technicien')->get();

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

print_r($results -> toJson());   
exit;

将以下输出返回给我:

[{
    "nom": "Ravi-carpenter",
    "moyenne_avis": 2,
    "tache": "Task #1",
    "tarif": 5.22,
    "avg_avis_interventions": 2
}, {
    "nom": "Ravi-carpenter",
    "moyenne_avis": 3.5,
    "tache": "Task #2",
    "tarif": 6.52,
    "avg_avis_interventions": 3
}]

答案 1 :(得分:0)

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

        return $tarifications->map(function ($tarification) {
            return [
                'nom' => $tarification->technicien->user->nom,
                'tache' => $tarification->tache_id,
                'tarif' => $tarification->tarif,
                'avg_avis_interventions' => $tarification->technicien- 
                 >avisinterventions->avg('note')
            ];
        }); 

        print_r($results -> toJson());   
        exit;

   }