代码之家  ›  专栏  ›  技术社区  ›  IT2704

json:如何添加avg以获取平均值

  •  0
  • IT2704  · 技术社区  · 6 年前

    我有一个显示“TarificationTaches”列表的函数,我想添加一个avg以从表avis_intervention where“intervention.technicien_id”=“TarificationTaches.technicien_id”中获取平均“techniciens”moyenne avis,in this is my schema.

    Taches_Table

    public function up(){
    架构::创建(“附件”,函数(蓝图$表){
    $table->增量(“id”);
    $table->字符串(“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->时间戳();
    })(二)
    }
    

    TarificationTaches_Tables

    schema::create('tarificationTaches',函数(blueprint$table){
    $table->增量(“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('techniien_id')->references('id')->on('techniiens');
    $table->datetime('deleted_at')->nullable();
    $table->时间戳();
    })(二)
    

    技术表格

    schema::create('techniciens',function(blueprint$table){
    $table->增量(“id”);
    $table->布尔值(“actif”)->默认值(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->时间戳();
    
    })(二)
    

    avis_interventions_tables

    schema::create('avis_interventions',function(blueprint$table)'{
    $table->增量(“id”);
    $table->字符串(“qualit”);
    $table->整数(“nbr_heure”);
    $table->字符串(“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->时间戳();
    })(二)
    

    干预措施表

    schema::create('interventions',function(blueprint$table))。{
    $table->增量(“id”);
    $table->日期(“日期干预”)->可以为空();
    $table->字符串(“description”);
    $table->日期时间(“duree_prevu”);
    $table->布尔值(“statut”);
    $table->integer('technicien_id')->unsigned();
    $table->foreign('techniien_id')->references('id')->on('techniiens');
    $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->时间戳();
    })(二)
    

    这是我的功能

    public function gettar(){
    $Tarifications=TarificationTache::with('technicien')->get();
    
    返回$Tarifications->映射(函数($Tarification){
    返回[
    'nom'=>$Tarification->技术人员->用户->nom,
    'Moyenne Avis'=>$Tarification->技术人员--gt;Moyenne Avis,
    'Tache'=>$Tarification->Tache->libelle_-Tache,
    'Tarif'=>$Tarification->Tarif,
    ](二)
    })(二)
    
    
    }
    

    如图所示

    [“nom”:“tech 1”,“moyenne-avis”:空,“tache”:“tache 2”,“tarif”:29.55,
    “nom”:“技术
    2“,”Moyenne_Avis“:空,”“Tache”:“Tache 3”,“Tarif”:55.12,”Nom“:技术
    1“,”Moyenne_Avis“:空,”“Tache”:“Tache 3”,“Tarif”:253,”Nom“:技术
    2“,”Moyenne_Avis“:空,”“Tache”:“Tache 3”,“Tarif”:28.22]
    

    =

    转速表

    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表

    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();
    });
    

    技术表

        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干预表

        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();
    });
    

    干预措施表

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

    2 回复  |  直到 6 年前
        1
  •  0
  •   chagou2704    6 年前
      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;
    
       }
    
        2
  •  1
  •   Ravi Maniyar    6 年前

    编辑:

    <?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_干预模式:

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