Laravel query join as json

Published 2 months ago by chagouani

I'm trying to build one service json Laravel. I want to show liste of tarificationtaches groupe by tache .

table techniciens

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

table users

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email');
        $table->string('password');
        $table->string('nom');
        $table->string('prenom');
        $table->string('tel');
        $table->string('mobil');
        $table->boolean('role')->default(0);
        $table->datetime('deleted_at')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });

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

function

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

i have this function it shown like this

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

but i would like to have the 'nom' of technicien not the 'technicien_id'

id: N,
tache_id: "tache",
technicien_id: 5
     nom: "user.nom",
     

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