json

Published 2 months ago by chagouani

I have a function that displays the 'tarificationtaches' list i would like to add an avg to get the average 'techniciens' moyenne_avis from tables avis_intervention where "intervention.technicien_id" = "tarificationtaches.techncien_id" , in this is my schema .

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

this is my function

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,
            ];
        });    


   
}

it shown like this

[{"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}]

NB: the moyenne_avis is null because he display moyenne_avis from technicien and i dont have any insert on .

Please sign in or create an account to participate in this conversation.