Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chagouani's avatar

how to display the average rate of "tarifs" for a selected tache

i have 2 tables the first tache 'id','libelle_tache' , 'tarif' and table tarification tache 'tache_id', 'tarif' .I would like to display in the interface tache.show the average rate of "tarifs" of this selected tache for all technicien.

@extends('Layouts/app')
@extends('Layouts.master')
@section('content')
@if(count($errors))
<div class="alert alert-danger" role="alert">
 <ul>
@foreach($errors ->all() as $message)
<li>{{$message}}</li>
@endforeach
</ul>
</div>
@endif
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>



<script type="text/javascript">

var getMetiersByTechnicienUrl = "{{url('/metiersbytechnicien')}}";
var getTachesByMetierUrl = "{{url('/tachesbymetier')}}";

//console.log(getMetiersByTechnicienUrl,getTachesByMetierUrl,
getTarificationsByTacheUrl);
 function getMetiersByTechnicien(val) {
if(val.length>0) {
var technicien_id = val;
$.get(getMetiersByTechnicienUrl+'/'+technicien_id,function(res) {
    var html = '<option value="">-Select-</option>' ;
    $.each(res.metiers,function(index,item) {
        html+='<option value="'+item.id+'">'+item.libelle_metier+'</option>';
    });
    $('#metiers').html(html);

 });
    }
 }
function getTachesByMetier(val) {
 if(val.length>0) {
var metier_id = val;
$.get(getTachesByMetierUrl+'/'+metier_id,function(res) {
    var html = '<option value="">-Select-</option>' ;
    $.each(res.taches,function(index,item) {
        html+='<option value="'+item.id+'">'+item.libelle_tache+'</option>';
    });
    $('#taches').html(html);

});
}
}

</script>
<div class="container">
<div class="row"></div>
<div class="col-md-12">
Z
<div class="col-md-10">
<h1>Tarification tache</h1>
<form action=" {{url ('tarification')  }}" method="post">
 {{csrf_field()}}

  <div class="form-group">
  <label for="technicien">Technicien</label>
    <select onchange="getMetiersByTechnicien(this.value)" name="technicien_id" 
 id="technicien" class="form-control">
        <option value="">-Select-</option>
        @foreach($technicien as $t)
            <option value="{{$t->id }}">
                {{$t->user->nom}}
            </option>
        @endforeach
    </select>
    </div>
    <div class="form-group">
    <div class="col-md-12">
        <div class="col-md-4">
    <label>Metier: </label>
    <select onchange="getTachesByMetier(this.value)" style="width: 200px"    
    class="productm form-control" id="metiers">
     <option value="">-Select-</option>


    </select>
        </div>
        <div class="col-md-4">
            <label>tache: </label>
            <select style="width: 200px" class="productname form-control" 
 name="tache_id" id="taches">
                <option value="">-Select-</option>
            </select>
        </div>
        <div class="col-md-4">
            <label>tarification: </label>
            <input style="width: 200px" class="productname form-control" type="text"  
  name ="Tarif" class="form-control" value="{{old('tarif')}}">
               
            
        </div>


    </div>
    </div>
    <div class="form-group">
    <input type="submit" value = "enregistrer" class="form-control btn btn-primary">
     </div>
    </div>
    </div>
 </div>

@endsection

tarificationcontroller:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\tarificationtache;
use App\technicien;
use App\tache;
use App\metier;

class TarificationController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$Listtache=tache::orderBy('libelle_tache')->get();
$Listtarification=tarificationtache::all();
return view('tarification.index',['tarification'=>$Listtarification]);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()

$technicien = technicien::orderBy('id','desc')->get();
$taches = Tache::orderBy('libelle_tache', 'asc')->get();
$metiers = Metier::orderBy('libelle_metier', 'asc')->get();
return view('tarification.create')->with('taches', $taches)->with('technicien', $technicien)- 
>with('metiers', $metiers);
}

/**
* Store a newly created resource in storage.
*
* @param  \Illuminate\Http\Request  $request
*@return \Illuminate\Http\Response
*/
public function store(Request $request)
{

$tarification = new tarificationtache();
$tarification ->tache_id = $request->input('tache_id');
$tarification ->Tarif =$request->input('Tarif');
$tarification->save();
$tarification->techniciens()->attach($request->technicien_id);
return redirect('technicien');  }

/**
* Display the specified resource.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$tache=Tache::find($id);
return view('tache.edit',['libelle_tache'=>$tache],['Tarif'=>$tache],['metier_id'=>$tache]);
}

/**
  * Update the specified resource in storage.
  *
* @param  \Illuminate\Http\Request  $request
* @param  int  $id
* @return \Illuminate\Http\Response
*/
   public function update(Request $request, $id)
  {
  //
}

/**
* Remove the specified resource from storage.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$tache =Tache::find($id);
$tache->delete();

 return redirect('tache');
}
public function getTarificationsByTache($tache_id)
{
$t =tache::find($tache_id);
return response()->json(['tarifications' => $t->tarificationtache]);
}

tache.show

@extends('Layouts/app')
@extends('Layouts.master')
@section('content')
 <div class="container">
    <div class="row">
    <div class="col-md-10">
  <h1 align="center">Detail Tache</h1>

 <div class="form-group">
        <label for="libelle_Tache">Libelle Tache</label>
        <label id="libelle_Tache" type="text" class="form-control"                      
 name="tache[libelle_Tache]" >{{$tache->libelle_tache}}</label>
    </div>
    <div class="form-group">
        <label for="Tarif">Tarif moyenne </label>
        <label id="Tarif" type="text" class="form-control" name="tache[Tarif]" >{{$tache-           
  >Tarif}}</label>
    </div>
    <div class="form-group">
        <label for="libelle_metier">Libelle Metier</label>
        <label id="prenom" type="text" class="form-control" name="metier[libelle_metier]" >         
 {$tache->metier->libelle_metier}}</label>
    </div>
   <div class="form-group" align="right">
        
                    <form action="{{url ('tache/'.$tache->id)}}" method="post">
                        {{csrf_field()}}
                        {{method_field('DELETE')}}
                        
                        <a href="{{url('/tache')}}" class="btn btn-default" class="btn btn-              
     primary">Retour</a>
                        <a href="{{url('tache/'.$tache->id.'/edit')}}" class="btn btn-default">Editer</a>
                        <button type="submit" class="btn btn-danger">Supprimer</button>
                    </form>
        </div>
     </div>
@endsection

tache controler

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
use App\Technicien;
class TacheController extends Controller
{

protected function validator(array $data)
{
return Validator::make($data, [
    'Tarif' => 'required|floatval(6.3)',
    
]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{


$tache=tache::with(['metier'])->get();

$search = $request->get('search');

$field = $request->get('field') != '' ? $request->get('field') : 'libelle_tache';
$sort = $request->get('sort') != '' ? $request->get('sort') : 'asc';
$tache = new tache();
if ($request)
    
$tache = $tache->where('libelle_tache', 'like', '%' . $search . '%')
    ->orderBy($field, $sort)
    ->paginate(5)
    ->withPath('?search=' . $search . '&libelle_tache=' . $tache . '&field=' . $field . '&sort=' .               
 $sort);
    return view('tache.index',['tache'=>$tache]);

 }

 /**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
*/
public function create()
{

//$metiers = Costcenter::lists('libelle_metier', 'id');
$metiers = Metier::orderBy('libelle_metier', 'asc')->get();
return view('tache.create')->with('metiers', $metiers);
}

/**
* Store a newly created resource in storage.
*
* @param  \Illuminate\Http\Request  $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tache = new Tache();
$tache ->libelle_tache =$request->input('libelle_tache');
$tache ->Tarif =$request->input('Tarif');
$tache ->metier_id = $request->input('metier_id');
$tache->save();
return redirect('tache');
}

/**
* Display the specified resource.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{

$tache = tache::findOrFail($id);
$metier = $tache->metier;
return view('tache.show' , compact('tache'))->with('metier',$metier);

}

/**
* Show the form for editing the specified resource.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$tache=Tache::find($id);
$metiers = Metier::orderBy('libelle_metier', 'asc')->get();
return view('tache.edit',['libelle_tache'=>$tache],['Tarif'=>$tache],['metier_id'=>$tache])-    
>with('metiers', $metiers);
}

public function update(Request $request, $id)
{
// do some request validation
$tache=Tache::find($id);
$tache->update($request->all());
return redirect('tache');
}

/**
* Remove the specified resource from storage.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$tache =Tache::find($id);
$tache->delete();

return redirect('tache');
}
public function getTachesByMetier($metier_id)
{
$t = Metier::find($metier_id);
return response()->json(['taches' => $t->taches]);
}
}
0 likes
0 replies

Please or to participate in this conversation.